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

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
主站蜘蛛池模板: 壶关县| 新宾| 手游| 邮箱| 永修县| 庆阳市| 许昌市| 根河市| 福安市| 鞍山市| 仙游县| 东兴市| 浮梁县| 城固县| 瑞金市| 新津县| 衡山县| 凤台县| 原阳县| 荔浦县| 桐柏县| 吉安市| 文山县| 常州市| 康马县| 陈巴尔虎旗| 抚顺县| 稷山县| 平武县| 汝州市| 济南市| 天台县| 吕梁市| 卓尼县| 北川| 曲松县| 临安市| SHOW| 教育| 潞城市| 淮阳县|