官术网_书友最值得收藏!

Methods for rounding the value

There are four different methods that convert a Rat value to an integer: round, ceiling, floor, and truncate.

The round method rounds the value according to the mathematical definition: the value is rounded towards the closest integer. We can see that in the following code snippet:

say 3.14.round; # 3
say 2.71.round; # 3

(Notice that the first dot separates the decimal part of the number, while the second dot is a method call.)

Negative values are also rounded so that the result is the closest integer number. We can see that in the following code snippet:

say (-3.14).round; # -3
say (-2.71).round; # -3

The truncate method just cuts the decimal part, regardless of the sign, as follows:

say 3.14.truncate; # 3
say 2.71.truncate; # 2

say (-3.14).truncate; # -3
say (-2.71).truncate; # -2

Finally, the pair of ceiling and floor methods rounds the number to the next or previous integer, as shown here:

say 3.14.ceiling; # 4
say 3.14.floor; # 3

say (-2.71).ceiling; # -2
say (-2.71).floor; # -3
主站蜘蛛池模板: 忻州市| 聂拉木县| 封丘县| 龙南县| 从化市| 云霄县| 武夷山市| 常山县| 邯郸市| 大新县| 射阳县| 墨脱县| 平昌县| 曲沃县| 剑河县| 南靖县| 昌平区| 富源县| 吉木萨尔县| 建昌县| 同仁县| 新郑市| 顺平县| 南宫市| 黄浦区| 铜山县| 峡江县| 上林县| 霍城县| 淮南市| 乌鲁木齐市| 堆龙德庆县| 库尔勒市| 棋牌| 焦作市| 深州市| 大英县| 玉溪市| 凌云县| 武穴市| 浙江省|