# Perl舍入错误再次出现

>有一个数字105;
>除以1000(结果0.105)
>粗略到2位小数应为：0.11

```use 5.014;
use warnings;
my \$i = 105;
printf "%.2f\n", \$i/1000;   #prints 0.10```

https://stackoverflow.com/a/1838885 @Sinan Unur说(6次评论)：

Use sprintf(“%.3f”, \$value) for mathematical purposes too.

```use 5.014;
use warnings;
use Math::BigFloat;
my \$i = 105;

Math::BigFloat->precision(-2);
my \$r = Math::BigFloat->new(\$i/1000);

say "\$r";   #0.10 ;(```

```use 5.014;
use warnings;
use bignum ( p => -2 );
my \$i = 105;
my \$r = \$i/1000;
say "\$r";   #0.10 ;(```

```use 5.014;
use warnings;
use Math::Round;
my \$i = 105;
say nearest(0.01, \$i/1000); #GREAT prints 0.11 :)```

```use 5.014;
use warnings;
my \$i = 105;
my \$f = \$i/1000;

say myround(\$f,2);  # 0.11

sub myround {
my(\$float, \$prec) = @_;
my \$f = \$float * (10**\$prec);
my \$r = int(\$f + \$f/abs(\$f*2));
return \$r/(10**\$prec);
}```

```N = 345
DISPLAY N        # Displays 345
DISPLAY (1.2) N  # Displays 3.45```

```my \$amount = 1.344;
my \$amount_rounded = sprintf "%.2f", \$amount + .005;
my \$amount2 = 1.345;
my \$amount_rounded2 = sprintf "%.2f", \$amount2 + .005;
say "\$amount_rounted   \$amount_rounded2";  # prints 1.34 and 1.35```

```use strict;
use warnings;
use feature qw(say);

my \$number = .105;
say "\$number";
printf "%.2f\n", \$number;   # Prints .10 which is incorrect
printf "%.2f\n", 3.1459;    # Prins 3.15 which is correct```

```use strict;
use warnings;
use feature qw(say);

my \$number = .1051;
say "\$number";
printf "%.2f\n", \$number;   # Prints .11 which is correct
printf "%.2f\n", 3.1459;    # Prints 3.15 which is correct```