You can use sprintf('%.2f', $foo) to round it. There's probably a module does this automatically, but creating one isn't hard. Just tie a variable.
{ package BlahBlah; use Tie::Scalar; @ISA = 'Tie::StdScalar'; sub STORE { ${+shift} = sprintf '%0.2f', pop } } tie my $foo, 'BlahBlah'; $foo = 1; print "$foo\n"; # 1.00 $foo = 1.005; print "$foo\n"; # 1.00 $foo = 1.025; print "$foo\n"; # 1.02 $foo = 0; print "$foo\n"; # 0.00
Juerd
- http://juerd.nl/
- spamcollector_perlmonks@juerd.nl (do not use).
In reply to Re: Fixed precision numbers
by Juerd
in thread Fixed precision numbers
by iburrell
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |