When the output data is displayed on the screen, if the data is large, more digits, it will look more laborious, there is a more intuitive way is to use the thousand, that is, every three digits display a comma, so you can quickly know the size of the number, not a bit to slowly count.
Happily, there are special functions in PHP that can do this task, and can automatically add thousands of points when outputting data.
String Number_format (float number [, int decimals [, String Dec_point, String thousands_sep]])
Number_format has four parameters, the first parameter is the number to output (floating-point type), this parameter is required, the following three parameters are optional, where two of the following parameters are either none or all
Number required. The number to format. If no other parameters are set, the number is formatted with no decimal point and comma (,) as the separator.
Decimals Optional. Specify how many decimal places. If this argument is set, the number (.) is used as the decimal point to format the digits.
Decimalpoint Optional. A string that is used as a decimal point.
Separator optional. A string that is used as a thousand separator. Use only the first character of the parameter. For example, "XYZ" outputs only "X".
String Number_format (
float number,//numbers to output
int decimals,//decimal digits, defaults to 0
String dec_point,//decimal point representation, defaults to.
String THOUSANDS_SEP//thousand-digit representations, implied,
)
Let's try an example.
echo Number_format (' 1234.56 ');
echo Number_format (' 1234.56 ', 1);
echo Number_format (' 1234.56 ', 2);
echo Number_format (' 1234.56 ', 3);
echo Number_format (' 1234.56 ', 2, '-', '/');
The results are as follows
1,235//Rounded
1,234.6//
1,234.56//
1,234.560//small digit insufficient, add 0
The 1/234-56//decimal symbol becomes/, and the dot symbol changes to-
Example
Number_format
<?php
$number = 1234.56;
中文版 notation (default)
$english _format_number = Number_format ($number);
1,235
French notation
$nombre _format_francais = Number_format ($number, 2, ', ', ');
1 234,56
$number = 1234.5678;
中文版 notation without thousands seperator
$english _format_number = Number_format ($number, 2, '. ', ');
1234.57
?>