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

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
主站蜘蛛池模板: 内江市| 藁城市| 莒南县| 万全县| 宣汉县| 博白县| 延津县| 法库县| 江西省| 克山县| 三原县| 丹江口市| 方正县| 阿拉善盟| 石景山区| 平果县| 怀来县| 广安市| 木里| 交口县| 香格里拉县| 清新县| 乌兰浩特市| 重庆市| 安吉县| 日土县| 富锦市| 揭西县| 洛川县| 呼玛县| 什邡市| 英德市| 普兰县| 云林县| 扶余县| 浦城县| 华安县| 邵东县| 云南省| 买车| 会宁县|