Output of `perldoc -q round`

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

Remember that `int()` merely truncates toward `0`. For rounding to a certain number of digits, `sprintf()` or `printf()` is usually the easiest route.

``````    printf("%.3f", 3.1415926535);       # prints 3.142
``````

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

``````    use POSIX;
\$ceil   = ceil(3.5);                        # 4
\$floor  = floor(3.5);                       # 3
``````

In 5.000 to 5.003 perls, trigonometry was done in the `Math::Complex` module. With 5.004, the `Math::Trig` module (part of the standard Perl distribution) implements the trigonometric functions. Internally it uses the `Math::Complex` 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:

``````    for (\$i = 0; \$i < 1.01; \$i += 0.05) { printf "%.1f ",\$i}

0.0 0.1 0.1 0.2 0.2 0.2 0.3 0.3 0.4 0.4 0.5 0.5 0.6 0.7 0.7
0.8 0.8 0.9 0.9 1.0 1.0
``````

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中的字符串和数值_caz28的专栏-CSDN博客_perl 字符串转数字

https://blog.csdn.net/caz28/article/details/7788746

2012年7月27日 ... 打印出的是1，因为\$b转换成整数是0. ... 把一个字符串转换成整数，也可以显式 进行，用int()函数。 ... perl 提取字符串的中数字（包括小数点）.

### How do you round a floating point number in Perl? - Stack Overflow

https://stackoverflow.com/questions/178539/how-do-you-round-a-floating-point-number-in-perl

How can I round a decimal number (floating point) to the nearest integer? e.g. 1.2 = 1 1.7 = 2. Share.

### perl常用字符串函数- emanlee - 博客园

https://www.cnblogs.com/emanlee/p/3962473.html

2014年9月9日 ... 14、hex(string)：把字符串形式的十六进制数据转换成十进制的整数；. 15、int( floatnum)：把浮点数去掉小数部分而转换成整数；.

### perl 字符串删除末尾几个字符_Perl中的字符串操作函数_ ...

https://blog.csdn.net/weixin_39995351/article/details/112822726

2020年12月31日 ... 14、hex(string)：把字符串形式的十六进制数据转换成十进制的整数；. 15、int( floatnum)：把浮点数去掉小数部分而转换成整数；.

### Perl | int() function - GeeksforGeeks

https://www.geeksforgeeks.org/perl-int-function/

Jun 25, 2019 ... int() function in Perl returns the integer part of given value. It returns ... The int() function does not do rounding. ... Passing positive decimal value.

### Perl的数值和字符串- 骏马金龙- 博客园

https://www.cnblogs.com/f-ck-need-u/p/9511844.html

2018年8月21日 ... 就算写的是整数，在内部也会转换成等效的浮点数类型保存。 但在perl内部，有些 运算会将浮点数转换成整型进行，而且也有integer编译器可以强制 ...

### perl中取整怎么做_百度知道

https://zhidao.baidu.com/question/1800258638193902187.html

2017-11-15 在Perl，怎么对浮点数取整; 2018-01-19 perl中int是什么意思; 2010-05 -30 perl 四舍五入函数; 2017-11-04 perl中有把字符串转成整数的函数吗 ...

### python 将当前时间转为整数或者浮点数-CSDN论坛

https://bbs.csdn.net/topics/392022982

python当中浮点数为float，整数为int，浮点数转换整数有四种方式，这四种方式 之间也有相应的区别，掌握了他们之间的不同，我们就可以根据实际情景选取合适 的 ...

### int - Perldoc Browser

https://perldoc.perl.org/functions/int

Please contact him via the GitHub issue tracker or email regarding any issues with the site itself, search, or rendering of documentation. The Perl documentation is ...

### Rounding numbers in Perl

https://squareperl.com/en/perl-round

Rounding numbers in Perl · Rounding to a few decimal places One of the ways how to round a number to few decimal places in Perl to use it sprintf("%. · Ways of  ...

### 十进制小数转化为二进制小数| 菜鸟教程

https://www.runoob.com/w3cnote/decimal-decimals-are-converted-to-binary-fractions.html

### perl 如何将字符串转换成浮点数?-CSDN论坛

https://bbs.csdn.net/topics/210059734

2008年3月27日 ... 但在perl内部，有些运算会将浮点数转换成整型进行，而且也有integer编译器可以 强制编译成整型。 perl整数的表示方式有3种方式： 123 0123 ...

### Perl 数据类型| 菜鸟教程

https://www.runoob.com/perl/perl-data-types.html

Perl 数据类型Perl 是一种弱类型语言，所以变量不需要指定类型，Perl 解释器会 根据上下文自动选择匹配类型。 Perl 有三 ... Perl 实际上把整数存在你的计算机中的 浮点寄存器中，所以实际上被当作浮点数看待。 ... \L, 强制将所有的字符转换为小写 .

### sprintf - Perldoc Browser

https://perldoc.perl.org/functions/sprintf

Perl does its own sprintf formatting: it emulates the C function sprintf(3), but ... with the given number %s a string %d a signed integer, in decimal %u an ...

### Perl Numbers

https://www.perltutorial.org/perl-numbers/

Perl integers. Integers are whole numbers that have no digits after the decimal points i.e 10 , -20 or 100 . In ...

### rounding to nearest integer

https://www.perlmonks.org/bare/?node_id=257206

Feb 3, 2020 ... Does Perl have a round() function? What about ceil() and floor()? Trig functions? Remember that int() merely truncates toward 0. For rounding ...

### Perl Cookbook , 2.3. Rounding Floating-Point Numbers

http://rtfm.vtt.net/perl/cookbook/ch02_04.htm

You want to round a floating-point value to a certain number of decimal places. This problem arises as a result of the same inaccuracies in representation that ...

### Rounding Floating-Point Numbers - Perl Cookbook [Book]

https://www.oreilly.com/library/view/perl-cookbook/1565922433/ch02s04.html

You want to round a floating-point value to a certain number of decimal places. This problem arises as a result of the same inaccuracies in representation that ...

### Perl printf format examples (reference page) | alvinalexander.com

https://alvinalexander.com/blog/post/perl/reference-page-perl-printf-formatting-format-cheat-sheet/

May 28, 2017 ... The following code demonstrates how to print integers with Perl, ... And here's the output from those printf floating-point (decimal) examples: