Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Floats with trailing zeros as a hash key

by jkahn (Friar)
on May 09, 2003 at 22:59 UTC ( #257028=note: print w/ replies, xml ) Need Help??


in reply to Floats with trailing zeros as a hash key

I am going to guess that my suggestion (below) is too big a rewrite for the code you're working on, but let me make it anyway:

Store prices as integer cents.

There are several reasons that C programs usually store prices as integer amounts of pennies, and then convert to dollars at the last minute:

  • Fractions of a penny don't make sense (cents?)
  • no roundoff issues

This might be one of those cases for you, too. 4520 pennies will never get rounded to 452 pennies, but 45.20 dollars (or euros) will get rounded to 45.2 by internal number-to-string conversions.

Just my $0.02 er, 2¢


Comment on Re: Floats with trailing zeros as a hash key
Re: Re: Floats with trailing zeros as a hash key
by hardburn (Abbot) on May 10, 2003 at 00:17 UTC

    Wow, that's a good idea. Wish I had heard it a week ago (:

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (11)
As of 2014-12-26 15:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (171 votes), past polls