- 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
推薦閱讀
- 手機安全和可信應用開發指南:TrustZone與OP-TEE技術詳解
- C語言程序設計案例教程(第2版)
- Mastering Kotlin
- GitLab Repository Management
- 基于差分進化的優化方法及應用
- Amazon S3 Cookbook
- Learning SciPy for Numerical and Scientific Computing(Second Edition)
- Visual Basic 6.0程序設計實驗教程
- R語言數據可視化:科技圖表繪制
- Python青少年趣味編程
- 零基礎輕松學C++:青少年趣味編程(全彩版)
- Java高并發編程詳解:深入理解并發核心庫
- 百萬在線:大型游戲服務端開發
- Software-Defined Networking with OpenFlow(Second Edition)
- Unreal Engine 4 Scripting with C++ Cookbook