Page 1 of 1

Output 05 instead of just 5

Posted: Sun Dec 04, 2005 8:42 pm
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!

Posted: Sun Dec 04, 2005 8:56 pm
by neophyte

Posted: Sun Dec 04, 2005 9:08 pm
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.

Posted: Sun Dec 04, 2005 9:17 pm
by neophyte
sprintf --- I'm always forgetting about sprintf...

Posted: Sun Dec 04, 2005 9:20 pm
by josh
Both functions would work equally as well, sprintf just does other stuff in addition to padding.

Posted: Sun Dec 04, 2005 10:29 pm
by John Cartwright
or you can set the attribute of the column UNSIGNED_ZEROFILL

Posted: Mon Dec 05, 2005 8:07 am
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! :)