Output 05 instead of just 5

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
seodevhead
Forum Regular
Posts: 705
Joined: Sat Oct 08, 2005 8:18 pm
Location: Windermere, FL

Output 05 instead of just 5

Post by seodevhead »

I have an integer type tablecolumn that I want to output always as two digits.

For instance.. if the number that needs outputted is 5, I want it to output as 05.

How can I do this? Thanks and take care!
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

sprintf("%04d", 17);

Change the 4 to how many total digits the number should be, 17 is the number to pad..



17 will become 0017 in this example


Note that this returns the value, if you want to output the value use printf() instead, with the same arguments.
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

sprintf --- I'm always forgetting about sprintf...
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Both functions would work equally as well, sprintf just does other stuff in addition to padding.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

or you can set the attribute of the column UNSIGNED_ZEROFILL
User avatar
seodevhead
Forum Regular
Posts: 705
Joined: Sat Oct 08, 2005 8:18 pm
Location: Windermere, FL

Post by seodevhead »

Thanks a ton guys for the great advice. Jcart's idea with the zerofill is a good one... But i'll also use sprintf for good measure on the front-end. Thanks again and take care! :)
Post Reply