ppeel has asked for the wisdom of the Perl Monks concerning the following question:

I need to add leading zeroes to a variable $nfc_amt, so I'm using sprint() to do that. however when $nfc_amt = 115, the value returned is 0000114. What am I doing wrong?

print "NFC_AMT before adding zeroes: $nfc_amt\n"; $nfc_amt = sprintf("%07d", $nfc_amt); print "NFC_AMT after adding zeroes: $nfc_amt\n";

This is the result I get:

NFC_AMT before adding zeroes: 115

NFC_AMT after adding zeroes: 0000114

Replies are listed 'Best First'.
Re: sprintf() returning incorrect value (%.0f)
by tye (Sage) on Jul 15, 2015 at 13:41 UTC
    % say "sprintf '%07.0f', 114.6" 0000115

    - tye        

      Using this syntax works! "sprintf ('%07.0f', $nfc_amt)". Thank you!

        While %d truncates (114.999... ⇒ 114), %f rounds (114.999... ⇒ 115).
Re: sprintf() returning incorrect value
by toolic (Bishop) on Jul 15, 2015 at 12:59 UTC
Re: sprintf() returning incorrect value
by choroba (Cardinal) on Jul 15, 2015 at 13:01 UTC
    How do you populate $nfc_amt? Are you sure it's really 115 integer?
    my $n = 114.9999999999999; print "$n\t", sprintf "%07d\n", $n; __END__ Output: 115 0000114
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      I retrieve it from a DB2 table.

Re: sprintf() returning incorrect value
by hippo (Archbishop) on Jul 15, 2015 at 12:59 UTC

    Works fine for me:

    $ perl -e ' > $nfc_amt = 114; > print "NFC_AMT before adding zeroes: $nfc_amt\n"; > $nfc_amt = sprintf("%07d", $nfc_amt); > print "NFC_AMT after adding zeroes: $nfc_amt\n"; > ' NFC_AMT before adding zeroes: 114 NFC_AMT after adding zeroes: 0000114

    How are you setting $nfc_amt in the first place?

Re: sprintf() returning incorrect value
by Anonymous Monk on Jul 15, 2015 at 12:58 UTC

    Can't reproduce:

    my $nfc_amt = 115; print "NFC_AMT before adding zeroes: $nfc_amt\n"; $nfc_amt = sprintf("%07d", $nfc_amt); print "NFC_AMT after adding zeroes: $nfc_amt\n"; __END__ NFC_AMT before adding zeroes: 115 NFC_AMT after adding zeroes: 0000115

    Likely it doesn't contain what you think it contains. Could you add this to your code and show us the output: use Devel::Peek; Dump($nfc_amt);

      Added the Dump and this is the result: NFC_AMT before adding zeroes: 115 SV = NFC_AMT after adding zeroes: 0000114 PVNV(0x24cb13c) at 0x256709c REFCNT = 1 FLAGS = (PADMY,NOK,POK,pNOK,pPOK) IV = 0 NV = 115 PV = 0x271042c "115"\0 CUR = 3 LEN = 36

Re: sprintf() returning incorrect value
by flexvault (Monsignor) on Jul 15, 2015 at 18:10 UTC

    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

Re: sprintf() returning incorrect value
by pme (Monsignor) on Jul 15, 2015 at 13:03 UTC
    Hi ppeel

    Welcome to the monastery!

    UPDATE: Sorry, I totally misread your problem.

    You got what you described. If you need a single leading zero you can get this way:

    $nfc_amt = "0$nfc_amt"; # or $nfc_amt = '0' . nfc_amt;