- 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
推薦閱讀
- C++程序設(shè)計(jì)教程
- Computer Vision for the Web
- Manga Studio Ex 5 Cookbook
- Software Testing using Visual Studio 2012
- Visual C++串口通信技術(shù)詳解(第2版)
- Elastic Stack應(yīng)用寶典
- WordPress Plugin Development Cookbook(Second Edition)
- Android 應(yīng)用案例開發(fā)大全(第3版)
- Building an RPG with Unity 2018
- D3.js 4.x Data Visualization(Third Edition)
- C語言程序設(shè)計(jì)教程
- C語言程序設(shè)計(jì)實(shí)驗(yàn)指導(dǎo) (第2版)
- Creating Mobile Apps with jQuery Mobile(Second Edition)
- 開源項(xiàng)目成功之道
- .NET 4.5 Parallel Extensions Cookbook