http://qs321.pair.com?node_id=11136991


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

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

Replies are listed 'Best First'.
Re^5: Padding with sprintf changing number (updated)
by haukex (Bishop) on Sep 24, 2021 at 14:44 UTC

    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.