...

View Full Version : display number with leading zeros



sweenster
06-06-2005, 10:44 PM
if there a PHP function that can display a number with 3 leading zeros?

example:

2 -> 002
7 -> 007
15-> 015
35-> 035
245 ->245

you get the idea...

marek_mar
06-06-2005, 11:10 PM
<?php
$num = 213; // I want it to be the length of 10
$num = sprintf('%010d', $num);
print $num;
?>

Kid Charming
06-06-2005, 11:11 PM
You can use str_pad() (http://us2.php.net/manual/en/function.str-pad.php). Note you will be converting your variable to a string type, of course.

marek_mar
06-06-2005, 11:52 PM
You would be converting it to stering as an int / float type would have the leading zeros cut off. In other words... str_pad might be faster.


$num = str_pad($num, 10, 0, STR_PAD_LEFT);



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum