php

Use of number_format() Function in PHP

The number_format() function is used to format the number of data. It is a built-in function of PHP that is also used to format a number with grouped thousands. It returns a string value based on the parameters used in this function. The purposes of using different parameters of this function and how this function can be used to format the number have been shown in this tutorial.

Syntax:

The number_format() function can be used in two ways. Two syntaxes of this function are given below:

  • string number_format (float $number [, int $decimals = 0 ])
  • string number_format ( float $number , int $decimals = 0 , string $dec_point = “.” , string $thousands_sep = “,” ) 

According to the above syntaxes, the number_format() function can take either one, two, or four arguments. The comma(,) is used as the default thousand separators for both syntaxes.

When one argument is used in this function, it takes a floating number as the argument value.

When two argument values are used in this function, the first argument takes a floating number, and the second argument takes an integer number that will be used to set the number after the decimal point.

When the four arguments are used in this function, the first two arguments take the same values as the previous arguments mentioned above. The other two arguments are used to set the decimal point and the thousand separator.

Example 1: Use number_format() with one argument

The following example shows the use of the number_format() function with one argument to format the number with the default thousand separator. Create a PHP file with the following script.

The script will print the value of $number by adding thousand separators.

<?php

        //Set the number value

        $number = 5000000;

        //Print the formatted value

        echo "<br /><h3>The number after formatting is: <span style='color:blue'>".number_format($number)."</span></h3>";

?>

Output:

The following output will appear after running the script from the webserver. Here, the number 5000000 is printed with the thousand separators, and the value is 5,000,000.

Example 2: Use number_format() with two arguments

The following example shows the use of the number_format() function with two arguments to format the number with the default thousand separator and the decimal value.  Create a PHP file with the following script.

Here, a float number is used for formatting. The first number_format() function is used with one argument and it will print the default formatting. The second number_format() function is used with two arguments and 2 is used as a decimal value. The second function will format the number by taking two numbers after the decimal point.

<?php

        //Set the number value

        $number = 35678.67345;

        //Print the original number

        echo "<h3>The original number is: <span style='color:red'>".$number."</span></h3>";

        //Print the formatted value without decimal value

        echo "<h3>The output before formatting with decimal value is: <span style='color:green'>".number_format($number)."</span></h3>";

        //Print the formatted value with decimal value

        echo "<h3>The output after formatting with decimal value is: <span style='color:blue'>".number_format($number,2)."</span></h3>";

?>

Output:

The following output will appear after running the script from the webserver. In the output, the first line shows the original value of the number, the second line shows the default formatting, and the third line shows the formatted number with the two numbers after the decimal point.

Example 3: Use number_format() with four arguments

The following example shows the use of the number_format() function with four arguments to format the number with the defined thousand separator, decimal point, and decimal value.  Create a PHP file with the following script.

A float number is defined in the script. The number_format() function is used two times in the script. This function is used with two arguments and four arguments in the script to show the purpose of using the four arguments of this function. When the number_format() is used with four arguments, then ‘,’ is used for the decimal point, and ‘.’ Is used for the thousand separator.

<?php

        //Set the number value

        $number = 875620.7854;

        //Print the original number

        echo "<h3>The original number is: <span style='color:red'>".$number."</span></h3>";

        //Print the formatted value with two arguments

        echo "<h3>The output after formatting with two arguments is: <span style='color:blue'>".number_format($number,3)."</span></h3>";

        //Print the formatted value with four arguments

        echo "<h3>The output after formatting with four arguments is: <span style='color:blue'>".number_format($number,3,',','.')."</span></h3>";

?>

Output:

The following output will appear after running the script from the webserver. Here, the first line shows the original value of the number. The second line shows the formatted number with 3 digits after the decimal point, default decimal point, and default thousand separators. The third line shows the formatted number based on the character defined in the third and fourth argument of the function.

Example 4: Format number based on the conditional statement

The following example shows the way to format the number based on the conditional statement using the number_format() function. Create a PHP file with the following script.

Here, a float number is declared and the explode() function is used to retrieve the fractional part from the number. ‘if’ condition is used to check the number of digits of the fractional part and format the number based on this value.

<?php

       //Set a floating number

        $number = 45.158;

        //Create array based on decimal point

        $num_part = explode('.',$number);

        //Read the fractional value

        $fraction = $num_part[1];

        //Count the total digits of the fractional value

        $count = strlen((string)$fraction);

        //Format number based on the length of the fractional value

        if ($count >= 4)

                echo "The formatted value is: ".number_format($number, 3);

        else if($count >= 3)

                echo "The formatted value is: ".number_format($number, 2);

        else

                echo "The formatted value is: ".number_format($number, 1);

?>

Output:

The following output will appear after running the script from the webserver. Here, the number contains 3 digits after the decimal point. According to the script, the second conditional statement is true. So, the number is formatted with 2 digits after the decimal point.

Conclusion

Different uses of the number_format() function have been explained in this tutorial by using various examples. How this function works for one, two, and four arguments have been shown using different float numbers. Hopefully, the readers will be able to use this function properly to format the numbers after reading this tutorial.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.