×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

sprintf() not returning expected result

sprintf() not returning expected result

sprintf() not returning expected result

(OP)
I have a variable $nfc_amt that I want to add leading zeroes to. The value of $nfc_amt is 115.
my code:
print "NFC_AMT before adding zeroes: $nfc_amt\n";
$nfc_amt = sprintf("%07d", $nfc_amt);
print "NFC_AMT after adding zeroes: $nfc_amt\n";

my result:

NFC_AMT before adding zeroes: 115
NFC_AMT after adding zeroes: 0000114

What am I doing wrong?

Thanks in advance for your help.

RE: sprintf() not returning expected result

I imagine it's getting changed somewhere else. If I run the code you posted, I get the following results:

CODE

NFC_AMT before adding zeroes: 115
NFC_AMT after adding zeroes: 0000115 

RE: sprintf() not returning expected result

Another thought is, are you suing the sprintf statement to do rounding of a floating point number? That could be causing problems.

RE: sprintf() not returning expected result

(OP)
I actually retrieve this number from a DB2 table, and then try to add the leading zeroes.
When I changed the syntax to:
$nfc_amt = sprintf("%07.0f", $nfc_amt);
The Result is correct. So there is something in $nfc_amt that I'm not seeing.
This is a little disconcerting to me. I was comfortable using "%07d" and now I'm not sure it's always working. Out of a file of 6000 records, only 200 were returning the incorrect amount when using "%07d".

RE: sprintf() not returning expected result

What is the data type of that field in your database? If you print everything without using printf/sprintf (so, just using print) are the values what you expect (without the leading zeros)?

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close