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

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
主站蜘蛛池模板: 山东省| 荔波县| 和林格尔县| 宜州市| 涿州市| 灵川县| 永登县| 浑源县| 嘉荫县| 武宣县| 咸阳市| 宿州市| 拜泉县| 德惠市| 新沂市| 容城县| 遵化市| 土默特右旗| 黔南| 正镶白旗| 松江区| 邢台县| 伊宁市| 克拉玛依市| 克东县| 磴口县| 庄浪县| 白山市| 府谷县| 平原县| 固镇县| 双城市| 旬阳县| 梅河口市| 拉萨市| 广丰县| 永靖县| 泰兴市| 韶山市| 格尔木市| 鲁山县|