in reply to Fixed precision numbers
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).
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Fixed precision numbers
by dash2 (Hermit) on Feb 25, 2003 at 22:10 UTC | |
by Juerd (Abbot) on Feb 25, 2003 at 22:39 UTC | |
by dash2 (Hermit) on Feb 25, 2003 at 23:15 UTC |