Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: strange rounding

by reneeb (Chaplain)
on Nov 29, 2004 at 13:06 UTC ( [id://410912]=note: print w/replies, xml ) Need Help??


in reply to strange rounding

in these nodes the same topic was discussed: Strange rounding Error in Perl, Bug? 1+1 != 2

Replies are listed 'Best First'.
Re^2: strange rounding
by Anonymous Monk on Nov 29, 2004 at 16:03 UTC
    Well,
    I first unsuccessfuilly serached for int(), because I could not imagine
    that the division of 100 causes this,
    second I thought, that other must have been unpleased as well.

    Finally I want to let you know that even

    $u = sprintf "%.2f", (int(0.5+($u*$p*100))/100);
    produces 123.99 and not 124.00. Only Math::Round had the correct answers.

    Carl

      Try this:

      print int(0.500000000000001+($u*$p*100))/100;

      Examine what is said, not who speaks.
      "But you should never overestimate the ingenuity of the sceptics to come up with a counter-argument." -Myles Allen
      "Think for yourself!" - Abigail        "Time is a poor substitute for thought"--theorbtwo         "Efficiency is intelligent laziness." -David Dunham
      "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
        Nope,

        that doesn't give correct results!

        The problem is NOT within int(..) (I tried 0.501..)
        the result of int is correct 12400, but if you devide this
        (try only10!) you will get 123.99:

        $cent = int(0.5 + ($u*$p*100); # => 12400, ok #but $dollar = $cent / 100; # => 123.99 # even if you use sprintf (which itself does # NOT round financially, you have to it yourself! $dollar = sprintf ".2f", $cent/100; # => 123.99 Dollar
        Hmm

        Carl

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://410912]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-04-23 19:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found