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

Re: number comparison with a twist

by pryrt (Prior)
on Mar 02, 2020 at 14:12 UTC ( #11113633=note: print w/replies, xml ) Need Help??

in reply to number comparison with a twist

I am assuming you know What Every Computer Scientist Should Know About Floating-Point Arithmetic, since you say "this fails due to the way floats are stored in memory". On the off-chance you (or a future reader) needs a refresher, I have included the link.

Since the prices are coming as floats-in-a-string, you have it easy: you should just be able to strip out the decimal point. Assuming it's always got exactly two digits following, $price_string =~ s/\.(\d{2})$/$1/;. After this, it will be in string and numeric form as integer cents. parv already posted a regex that just strips the decimal point from anywhere in the string.

Alternately, don't use the int, which does truncation; instead, use a round-to-nearest, like POSIX#round

Replies are listed 'Best First'.
Re^2: number comparison with a twist
by anotherguest (Novice) on Mar 02, 2020 at 15:29 UTC

    I did not at all consider this solution, unfortunately, that also means I posted a too simplified prototype.

    The API can return the price with variable leading and trailing digits, so 19.900000 is a valid result as is 123.2 so a bit of an extension is needed:

    $price_string .= '0'; $price_string =~ s,^(\d+)\.(\d{2})0*,${1}${2},;
        Is it possible? I guess it is. After all, I have no control over the API. Then again, the prizes I get are end prizes including VAT so they MUST always be describable in whole cents. This is also the reason the database was set up to store them as integer cents, to avoid any rounding issues (that worked very well, right?).
        So, truncating to 2 decimal digits is The Right Thing(TM) in this case.
      > The API can return the price with variable leading and trailing digits, so 19.900000 is a valid result as is 123.2

      In this case my last reply should be perfect. :)

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2020-06-07 07:02 GMT
Find Nodes?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?

    Results (42 votes). Check out past polls.