Forum Moderators: coopster

Message Too Old, No Replies

format a number...

Add a few 0 before the number

         

talannon

7:58 pm on Mar 8, 2005 (gmt 0)

10+ Year Member



Hi,

I was wondering if there is a way to format a number to add 1 or 2 '0' before the actual number.

For example, if I have the number '1', I would want it to print it to look like '001' instead of just '1'. Same thing for '12' would look like '012'...

Or do I have to add manually the 0 before?

Thank you,

jusdrum

8:14 pm on Mar 8, 2005 (gmt 0)

10+ Year Member



You can do this with printf

Example:
$Number = 1;
printf("%03d",$Number);

should yield:

001

%03d explanation:
0 = pad with a zero
3 = fill it up to 3 columns
d = it's an integer

You can tweak that to your needs.

Read more at [php.net...]

talannon

3:52 pm on Mar 9, 2005 (gmt 0)

10+ Year Member



ahh thank you!

jatar_k

6:04 pm on Mar 9, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



another option would be [php.net...]