I have to print a numeric value which is less than 10 in the following format:
for 0 I want to print it 00
for 1 I want to print it 01
for 2 I want to print it 02
Are there any php library functions which will format numbers in this way?
$i = 1;
printf('%02d', $i);
The sprintf
page also documents the syntax for formatting specifiers (%02d
), so you can also see what other options are available.
printf('%02u', $number)
You can check if the value is less than ten and concatenate using the dot operator '.', i.e.
if($i < 10 && $i >0) print '0'.$i;
else print $i;