> Hi list, > > I am just wondering if I sumbled upon an error in perl an error in my > logic > or somehtign else see the below perl one liners > > $ perl -e '$n = 0.945; $r = sprintf("%.2f", $n); print "$r\n";' > 0.94 > > $ perl -e '$n = 0.9451; $r = sprintf("%.2f", $n); print "$r\n";' > 0.95 > > $ perl -e '$n = 0.9450; $r = sprintf("%.2f", $n); print "$r\n";' > 0.94 > > $ perl -e '$n = 0.94500; $r = sprintf("%.2f", $n); print "$r\n";' > 0.94 > > $ perl -e '$n = 0.945001; $r = sprintf("%.2f", $n); print "$r\n";' > 0.95 > > Now I expected perl to round the number: 0.945 to 0.95 and not to 0.94 > like > it is doing. I have no idea why it is doing this but I sure don't like > it. > The problem is I have to do some VAT calculations on thousands of > invoices > and invoice elements. I found that in rare cases I'm of by 1 cent or > even 3 > cents if there are lots of elements. Going over the logic I ran into > this > problem. > > Cany anyone advise me how to deal with this as I need to find a way to > round > correctly in all cases not just in cases where Perl decides it is a > good > idea to do so. :-) > > Regards,
This may be of interest; http://faq.perl.org/perlfaq4.html#Does_Perl_have_a_rou -- Owen -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/