Page 1 of 1

Adding a leading zero to a single digit number

Posted: Tue Feb 25, 2003 8:22 pm
by Matt Phelps
I've browsed through the manual but can't find a way of doing this. What I'm trying to achieve is to always have a 2 digit number - so that whenever a single digit is found (0,1,2,3,5,6,7,8,9) then they will be converted to 00,01,02 etc.

This is what I came up with but it doesn't seem to work. :(

Code: Select all

$gridnumber = '8';
		//adds a leading zero to single digits 1 to 9)
			$num_of_numbers = count_chars($gridnumber);
			if ($num_of_numbers == '1')
				{
				$gridnumber_array[] = '0';
				$gridnumber_array[] = $gridnumber;
				$gridnumber = implode('', $gridnumber_array);
				}
Any suggestions?

Posted: Tue Feb 25, 2003 9:00 pm
by Stoker
$formattedstring = sprintf('%02d',$integer);

Posted: Tue Feb 25, 2003 9:06 pm
by Matt Phelps
Thanks :)