Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Proper monetary rounding

by jonadab (Parson)
on Mar 05, 2007 at 12:49 UTC ( #603203=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Proper monetary rounding
in thread Proper monetary rounding

I think he's saying that sprintf actually does that, in just that way:

perl -e 'my $x = 1.5050; my $y = 1.5150; my $s = sprintf "%0.2f\n%0.2f +\n", $x, $y; print $s;' 1.50 1.52

"Normal" (gradeschool) rounding would produce 1.51 and 1.52 there, but sprintf says 1.50 and 1.52, which seems an aweful lot like round-to-even to me. How does it differ from what you wanted?

As far as accountants being picky about this... have you ever seen the movie Superman 3?

-- 
We're working on a six-year set of freely redistributable Vacation Bible School materials.


Comment on Re: Proper monetary rounding
Download Code
Re^2: Proper monetary rounding
by monarch (Priest) on Mar 05, 2007 at 16:00 UTC
    Are you sure that's sprintf behaviour, or is there a limitation in the IEEE floating point representation of 1.5050 that has it leaning on the low side of 0.0050? That's the only explanation I can think of as to why sprintf would be broken like this.
      Are you sure that's sprintf behaviour, or is there a limitation in the IEEE floating point representation of 1.5050 that has it leaning on the low side of 0.0050?

      I'm not personally sure of any of this, no. As far as I know it could depend on the C compiler perl was compiled with, the C library it was compiled against, and the day of the week it was compiled. I was just asking how the other poster's explanation of sprintf's behavior differed from the desired rounding behavior.

      On closer inspection, I think I see a difference I didn't notice before, though.

      -- 
      We're working on a six-year set of freely redistributable Vacation Bible School materials.
Re^2: Proper monetary rounding
by eric256 (Parson) on Mar 05, 2007 at 18:35 UTC

    printf("%0.2f\n", $_) for 1.4050, 1.5050, 1.6050, 1.7050; seems to indicate this only works for 1.5... or am i missing a bigger picture here?


    ___________
    Eric Hodges

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2014-11-27 18:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (186 votes), past polls