Home  >  Article  >  Backend Development  >  PHP number_format() function

PHP number_format() function

巴扎黑
巴扎黑Original
2016-11-24 13:59:511542browse

Format numbers The number_format and round
number_format() functions format numbers by grouping thousands.
Syntax: number_format(number,decimals,decimalpoint,separator)
Parameters                                                                                                                                                                             . The number to format. If no other parameters are set, numbers are formatted without a decimal point and with commas (,) as delimiters.
decimals                                                                               Optional. Specify the number of decimals. If this parameter is set, numbers are formatted using a period (.) as the decimal point.
decimalpoint Optional. Specifies the string used as the decimal point.
separator                             Optional. Specifies the string used as the thousands separator. Only the first character of the parameter is used. For example, "xyz" only outputs "x".
Note: If this parameter is set, all other parameters are required.
Tips and Notes
Note: This function supports one, two or four parameters (not three).

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

round() function rounds floating point numbers.

Syntax: round(x,prec)
Parameters                                                                                                          . Specifies the number to be rounded.
prec                                                                                                                                                                                                                                    Specifies the number of digits after the decimal point.
Description: Returns the result of rounding x according to the specified precision prec (the number of decimal digits after the decimal point). prec can also be negative or zero (default).
Note: PHP cannot handle strings like "12,300.2" correctly by default.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn