Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: number comparison with a twist

by pryrt (Abbot)
on Mar 02, 2020 at 14:12 UTC ( [id://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 round of POSIX

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?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11113633]
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: (2)
As of 2024-04-19 19:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found