in reply to Re: number comparison with a twist
in thread number comparison with a twist

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},;

Replies are listed 'Best First'.
Re^3: number comparison with a twist
by parv (Parson) on Mar 02, 2020 at 15:42 UTC
      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.
        A quibble: truncating is the wrong thing to do, and is the problem with your original int() solution in the first place. Rounding to the nearest cent is the right thing to do (which can be done through round() as I suggested, or using the sprintf() solutions that others have mentioned).

Re^3: number comparison with a twist
by LanX (Saint) on Mar 02, 2020 at 17:57 UTC
    > 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