PHP number_format of function definition and usage

  • 2020-05-16 06:36:37
  • OfStack

The number_format() function formats Numbers by grouping thousands.

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

A necessity. The number to format.

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

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

Optional. Specifies the string used as the thousands separator.

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

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

Hints and comments
Note: this function supports one, two, or four arguments (not three).
example
 
<?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 the actual test, the third and fourth parameters must exist at the same time, that is, either both of them are set or neither of them is set.

The third and fourth parameters are not set:

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

If you add up the Numbers, you only get a 13! .

The third and fourth parameters are set

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

At this time, the processing of this number after the operation of the word will be executed correctly!

The third parameter of this function indicates what the 'decimal point' position is represented by, either by default., or by other symbols such as','. Ps: but I don't think anyone would do that.
And the fourth one tells you what you're going to use to divide the Numbers every thousand places. If you don't have any special requirements, and you want to perform an operation, you'd better set it to null.

Related articles: