SoFunction
Updated on 2025-03-10

PHP number_format function definition and usage

The number_format() function formats numbers by grouping thousands of digits.

grammar
number_format(number,decimals,decimalpoint,separator)
parameter describe
number

Required. The number to format.

If no other parameters are set, the number is formatted without a decimal point and is a comma (,) as the separator.

decimals Optional. Specify how many decimals. If this parameter is set, the number (.) is used as the decimal point to format the number.
decimalpoint Optional. Specifies a string used as a decimal point.
separator

Optional. Specifies a string used as a thousand separator.

Use only the first character of this parameter. For example, "xyz" only outputs "x".

Note: If this parameter is set, all other parameters are required.

Tips and comments
Note: This function supports one, two, or four parameters (not three).
example
Copy the codeThe code is as follows:

<?php
echo number_format("1000000");
echo number_format("1000000",2);
echo number_format("1000000",2,",",".");
?>

Output:

1,000,000
1,000,000.00
1.000.000,00

Interesting number_format
number_format(number,decimals,decimalpoint,separator)

There are four parameters,

The first and second parameters are required, and the third and fourth are optional. However, in actual tests, the third and fourth parameters must exist at the same time, that is, they must either be set or neither.

No third and fourth parameters are set:

Number_format(13526, 2); echo 13,526.00;

If you accumulate this processed number, you will only get a 13! .

The third and fourth parameters are set

Number_format(23125, 2, ‘.',''); echo 23125.00;

At this time, if the processed numbers are calculated, they will be executed correctly!

The third parameter of this function indicates what the position of the ‘decimal point’ is used to represent. It can be defaulted. It can also be set to ‘,’ and other symbols such as ‘. Ps: But I believe no one will do that.
The fourth one represents what to use to divide numbers every thousand digits. If there are no special requirements and the calculation is required, it is best to set it to empty.