How to Format a Number with Leading Zero in PHP?

Problem:

You added a zero(0) in front of a number but when you display it, the zero doesn’t show. You’re looking for a way to show that zero.

Solution:

There are few ways to add a leading zero in front of a number. Let’s check the methods below-

Method 1: using sprintf() function

The following function shows how to do it using sprint() function-

<?php
$number = 889; // Length of the supplied number is 3
$number = sprintf('%04d', $number);
echo $number;
?>

[wpdm_file id=56]

Output:
0889

How it works:
sprint() is useful to convert a data from one format to another. It can take few parameters. For our purpose, we’ll use first 2 parameters only.

The first parameter is called format specifiers which set the rules on how we want our number to be formatted and the second parameter is our supplied number.

A Format specifier starts with a percent sign(%) and then there are one or more optional specifiers which must be written in order.

Remember that our supplied number(889) is 3 digits in length and adding a zero in front of it, it would be 4 in length. After % sign, the 0(zero) is a padding specifier which tells to add a zero in front of the $number. Then, the 4 is a width specifier which indicates the length of the resulting output would be 4. At last, the d is a type specifier which tells the function to treat the $number(889) as a decimal integer.

More sprint() Examples-

  • If the width specifier is equal to or less than the length of the supplied number, the resulting number will have no leading zero. See the example-
    <?php   $number = 889; // Length of the supplied number is 3
    $number = sprintf('%03d', $number);
    echo $number;
    ?>
    

    [wpdm_file id=57]

    Output:
    889

  • To add 2 leading zeros, set the width specifier to 2 more than the length of the supplied number. See the example:-
    <?php  
    $number = 889; // Length of the supplied number is 3  
    $number = sprintf('%05d', $number);  
    echo $number;
    ?>
    

    [wpdm_file id=58]

    Output:
    00889

Method 2: using printf() function

printf() function works in the same way as the sprint() function does. prints() function just prints the output directly whereas sprints() function facilitates to save the result in a variable. Both functions take the same format specifier.

See in the following example how the prints() function add a leading zero in front of our supplied number-

<?php
$number = 889;
$number = printf('%04d', $number);
echo $number;
?>

[wpdm_file id=59]

Output:
0889

Method 3: using str_pad() function

str_pad() is easier to understand than the previous methods. The following example also adds a leading zero of the number 889.

<?php
$number = 889;
$number = str_pad($number, 4, '0', STR_PAD_LEFT);
echo $number;
?>

[wpdm_file id=60]

Output:
0889

How it works:
The first parameter is the supplied number 889.

The second parameter is the desired length of output. Here, we want our output be 4 digits in length(which is 0889).

The third parameter specifies the string to be used as padding. Here it is zero(‘0’).

The fourth parameter specifies in which side we want to pad. Here, we want to pad in left side, so we used STR_PAD_LEFT.