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

  • Perl 6 Deep Dive
  • Andrew Shitov
  • 178字
  • 2021-07-03 00:05:51

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
主站蜘蛛池模板: 民丰县| 公主岭市| 温州市| 平远县| 南召县| 太和县| 蒲城县| 通许县| 平潭县| 盖州市| 南乐县| 云霄县| 襄城县| 宁南县| 涪陵区| 景洪市| 基隆市| 拉孜县| 天津市| 古丈县| 台北市| 平顺县| 石阡县| 正安县| 屏山县| 富裕县| 明水县| 五大连池市| 搜索| 青田县| 乌鲁木齐市| 逊克县| 凌源市| 丽江市| 梅州市| 尚志市| 郧西县| 大新县| 大安市| 滨海县| 平江县|