DutchCoder has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl -w use strict; my $ammount; my $value; my $response = qq{Value should be 189.81 (rounded to two decimals)} . qq{ and sprintf gives us: %.02f \n}; $ammount = 159.5; $value = $ammount * 1.19; print sprintf($response, $value); $ammount = 159.5; $value = ($ammount*119)/100; print sprintf($response, $value);
When using the first calculation, the value was off by 1 cent. This raised some eyebrows while evaluating our code.
Can someone shed a light on this behaviour?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: rounding variations after multiplication and division
by moritz (Cardinal) on Aug 06, 2009 at 12:30 UTC | |
by DutchCoder (Scribe) on Aug 06, 2009 at 12:34 UTC | |
|
Re: rounding variations after multiplication and division
by ig (Vicar) on Aug 06, 2009 at 12:35 UTC | |
by DutchCoder (Scribe) on Aug 06, 2009 at 12:46 UTC | |
|
Re: [solved] rounding variations after multiplication and division
by roboticus (Chancellor) on Aug 07, 2009 at 00:12 UTC |