Home  >  Article  >  Backend Development  >  Exploring the PHP number_format function principle and example analysis

Exploring the PHP number_format function principle and example analysis

coldplay.xixi
coldplay.xixiforward
2020-07-22 16:40:332519browse

Exploring the PHP number_format function principle and example analysis

Example 1

Format numbers:

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

Definition And usage

number_format() function formats a number by grouping by thousands.

Note: This function supports one, two or four parameters (not three).

Syntax

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

ParametersDescriptionnumberRequired. The number to format. If no other parameters are set, the number is formatted without a decimal point and with a comma (,) as the thousands separator. decimalsOptional. Specify the number of decimals. If this parameter is set, numbers are formatted using a period (.) as the decimal point. decimalpointOptional. Specifies the string used as the decimal point. separatorOptional. Specifies the string used as thousands separator. Only the first character of the parameter is used. For example, "xxx" only outputs "x".
Note: If this parameter is set, all other parameters are required.

Example 2

You want to return a price: one argument will round the number (formatted without decimal place form), the two parameters will give you the result you want:

<?php 
$num = 1999.9; 
$formattedNum = number_format($num).""; 
echo $formattedNum; 
$formattedNum = number_format($num, 2); 
echo $formattedNum; 
?>

Related learning recommendations:

PHP programming from entry to proficiency

The above is the detailed content of Exploring the PHP number_format function principle and example analysis. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:jb51.net. If there is any infringement, please contact admin@php.cn delete