Exploring the PHP number_format function principle and example analysis

coldplay.xixi
Release: 2023-04-09 10:34:02
forward
2482 people have browsed it

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,",","."); 
?>
Copy after login

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; 
?>
Copy after login
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!

Related labels:
source:jb51.net
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!