Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^2: Padding with sprintf changing number

by sachin raj aryan (Acolyte)
on Sep 24, 2021 at 11:37 UTC ( #11136989=note: print w/replies, xml ) Need Help??


in reply to Re: Padding with sprintf changing number
in thread Padding with sprintf changing number

This was the operation i was doing it before padding

print " Checking amnt before conversion $amt,\n"; $amt= sprintf("%.2f",$amt); print " Checking amnt after rounding $amt,\n"; $amt = $amt*100;

Replies are listed 'Best First'.
Re^3: Padding with sprintf changing number
by haukex (Bishop) on Sep 24, 2021 at 11:50 UTC

    4887.15 cannot be stored accurately as a floating-point number, which is may be is causing your issue. See my post above for more information.

      4887.15 cannot be stored accurately as a floating-point number, which is causing your issue.

      Could it be that (at least part of) the problem is that the OP is expecting that sprint("%d", 488714.999) will return the string 488715 ?
      It won't:
      C:\>perl -le "print sprintf('%d', 488714.999);" 488714

      Cheers,
      Rob

        Yes, it could also be that $amt is intentionally something like 4887.14999..., so my statement above may be a little too certain (edited). That in Perl, rounding is often done with sprintf's %f instead of %d is a good point.

        Update: Wait a minute - OP says they did $amt = sprintf("%.2f",$amt); before the $amt = $amt*100;, so the issue is pretty certainly that $amt was first something that rounds to 4887.15, which can't be represented accurately, and assuming $amt is the same as $amount in the root node, the cause of the issue is pretty clear.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2022-01-21 12:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (57 votes). Check out past polls.

    Notices?