The number_format() function formats a number with grouped thousands.
PHP number_format
PHP number_format() is a built-in function that formats the number with grouped thousands. The number_format() function accepts either one, two, or four parameters (not three). Then, it returns the formatted number.
Syntax
The syntax of number_format() function is following.
number_format(number, decimals, decimalpoint, separator)
Parameters
The number parameter is required, and the number is to be formatted. If no other parameters are set, the number will be formatted without the decimals and with the comma (,) as the thousands separator.
The decimals are optional, and it specifies how many decimals.
If this parameter is set, the number will be formatted with the dot (.) notation as the decimal point.
The decimalpoint is optional and specifies what string to use for a decimal point.
The separator is optional and specifies what string to use for the thousands separator. Only the first character of the separator is used. For instance, “xxx” will give the same output as the “x”. If this parameter is given, all other arguments must also be present.
Example
See the following code.
<?php echo number_format("1000000")."\n"; echo number_format("1000000",2)."\n"; echo number_format("1000000",2,",",".");
See the output.
➜ pro php app.php 1,000,000 1,000,000.00 1.000.000,00 ➜ pro
Let’s see another example.
<?php $data = 1111.1; $formattedData = number_format($data)."\n"; echo $formattedData; $formattedNum = number_format($data, 2); echo $formattedNum;
See the output.
➜ pro php app.php 1,111 1,111.10 ➜ pro
If you pass anything instead of numbers, it gives a warning. See the code.
<?php $data = "ELEVEN"; echo number_format($data)."\n\n"; echo number_format($data, 3);
See the following output.
➜ pro php app.php PHP Warning: number_format() expects parameter 1 to be float, string given in /Users/krunal/Desktop/code/php/pro/app.php on line 5 Warning: number_format() expects parameter 1 to be float, string given in /Users/krunal/Desktop/code/php/pro/app.php on line 5 PHP Warning: number_format() expects parameter 1 to be float, string given in /Users/krunal/Desktop/code/php/pro/app.php on line 7 Warning: number_format() expects parameter 1 to be float, string given in /Users/krunal/Desktop/code/php/pro/app.php on line 7 ➜ pro
French notation
See the following code, which represents the number in french format.
<?php $number = 1111.21; echo number_format($number, 2, ',', ' ');
See the output.
➜ pro php app.php 1 111,21 ➜ pro
Be careful, when you’re using French notation
means : number_format(124.25, 2 , ‘,’ , ‘ ‘) with ‘,’ as dec_point,
Don’t forget to specify thousands_sep that default is ‘,’ to another value; otherwise, the function will return null.
Transforming PHP string to two decimal places
See the following code, which transforms PHP String to two decimal places. It removes other digits after two decimal places.
<?php $number = 1111.1111; echo number_format((float)$number, 2, '.', '');
See the output.
➜ pro php app.php 1111.11 ➜ pro
That’s it for this tutorial.