Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: sprintf() returning incorrect value

by flexvault (Monsignor)
on Jul 15, 2015 at 18:10 UTC ( [id://1134941]=note: print w/replies, xml ) Need Help??


in reply to sprintf() returning incorrect value

Welcome ppeel,

This may be my experience only, but I've had problems keeping leading zeros ('0'), in a variable ( especially hashes and arrays ) within a long running script. So instead of using 'sprintf', I usually add a number with the correct number of zeros, and then use 'substr' to remove it when I need to print, etc. So your:

$nfc_amt = sprintf("%07f", $nfc_amt);
becomes for me:
$nfc_amt = $nfc_amt + 10000000; # or $nfc_amt += 10000000;
Then when I need to print/etc. the result, I do:
print "NFC_AMT after adding zeroes: ",substr($nfc_amt,1),"\n";
I have only experienced this when I add the zeros and then use the variables much later. YMMV. (Note: It also could have been a bug in the version of Perl that I was using which is now fixed. :-)

Regards...Ed

"Well done is better than well said." - Benjamin Franklin

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2024-04-25 20:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found