Adding a leading zero to a single digit number

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

Post Reply
Matt Phelps
Forum Commoner
Posts: 82
Joined: Fri Jun 14, 2002 2:05 pm

Adding a leading zero to a single digit number

Post 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?
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post by Stoker »

$formattedstring = sprintf('%02d',$integer);
Matt Phelps
Forum Commoner
Posts: 82
Joined: Fri Jun 14, 2002 2:05 pm

Post by Matt Phelps »

Thanks :)
Post Reply