你如何在Perl中舍入一个浮点数?

前端之家收集整理的这篇文章主要介绍了你如何在Perl中舍入一个浮点数?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何舍入十进制数(浮点)到最接近的整数?

例如

1.2 = 1
1.7 = 2

解决方法

输出perldoc -q round

Does Perl have a round() function? What about ceil() and floor()?
Trig functions?

Remember that 07001 merely truncates toward 0. For rounding to a certain number of digits,07002 or 07003 is usually the easiest
route.

06000

The 07004 module (part of the standard Perl distribution) implements
ceil(),floor(),and a number of other mathematical and trigonometric
functions.

06001

In 5.000 to 5.003 perls,trigonometry was done in the 07005
module. With 5.004,the 07006 module (part of the standard Perl
distribution) implements the trigonometric functions. Internally it
uses the 07005 module and some functions can break out from the
real axis into the complex plane,for example the inverse sine of 2.

Rounding in financial applications can have serIoUs implications,and
the rounding method used should be specified precisely. In these
cases,it probably pays not to trust whichever system rounding is being
used by Perl,but to instead implement the rounding function you need
yourself.

To see why,notice how you’ll still have an issue on half-way-point
alternation:

06002

Don’t blame Perl. It’s the same as in C. IEEE says we have to do
this. Perl numbers whose absolute values are integers under 2**31 (on 32 bit machines) will work pretty much like mathematical integers. Other numbers are not guaranteed.

原文链接:/Perl/173598.html

猜你在找的Perl相关文章