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
seodevhead
Forum Regular
Posts: 705 Joined: Sat Oct 08, 2005 8:18 pm
Location: Windermere, FL
Post
by seodevhead » Sun Dec 04, 2005 8:42 pm
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!
neophyte
DevNet Resident
Posts: 1537 Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota
Post
by neophyte » Sun Dec 04, 2005 8:56 pm
josh
DevNet Master
Posts: 4872 Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida
Post
by josh » Sun Dec 04, 2005 9:08 pm
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.
neophyte
DevNet Resident
Posts: 1537 Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota
Post
by neophyte » Sun Dec 04, 2005 9:17 pm
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 » Sun Dec 04, 2005 9:20 pm
Both functions would work equally as well, sprintf just does other stuff in addition to padding.
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Sun Dec 04, 2005 10:29 pm
or you can set the attribute of the column UNSIGNED_ZEROFILL
seodevhead
Forum Regular
Posts: 705 Joined: Sat Oct 08, 2005 8:18 pm
Location: Windermere, FL
Post
by seodevhead » Mon Dec 05, 2005 8:07 am
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!