How to use php number_format function?

藏色散人
Release: 2023-04-05 21:42:02
Original
5043 people have browsed it

The number_format function is a built-in function in PHP, used to format a number containing thousands of groups. Its syntax is "string number_format ($number, $decimals, $decimalpoint, $sep)".

How to use php number_format function?

This article mainly introduces how to use php number_format. I hope it will be helpful to friends in need!

number_format() function is a built-in function in PHP, used to format a number containing thousands of groups. It returns the formatted number on success, otherwise gives E_WARNING on failure.

Syntax:

string number_format ( $number, $decimals, $decimalpoint, $sep )
Copy after login

Parameters:

This function accepts the above four parameters, as described below:

$number: It is a required parameter to specify the number to be formatted. If no other parameters are set, the number will be formatted without decimals and using a comma (,) as the thousands separator. The required argument specifies the number to be formatted.

$decimals: It is an optional parameter used to specify decimals. If this parameter is set, the number will be formatted using a period (.) as the decimal point.

$decimalpoint: It is an optional parameter that specifies the string to be used for the decimal point.

$sep: It is an optional parameter that specifies the string used for thousands separator. If this parameter is given, all other parameters are required.

Return value:

Returns a formatted number on success, and returns E_WARNING on failure.

number_format() code example 1:

<?php 
$num1 = "999999.49"; 
  
// 没有小数点参数 
echo number_format($num1)."\n"; 
  
// 带小数点参数 
echo number_format($num1, 3)."\n"; 
   
$num2 = "9999999.99"; 
  
// 没有小数点参数
//返回整数值
echo number_format($num2)."\n"; 
  
//带小数点参数
echo number_format($num2, 3)."\n";  
  
// 包含所有四个参数
echo number_format("1000000.99", 3, "#", "GGG"); 
  
   
?>
Copy after login

Output:

999,999
999,999.490
10,000,000
9,999,999.990
1GGG000GGG000#990
Copy after login

number_format() code example 2:

If you pass anything other than a number, it will give a warning.

<?php 
$num = "GFG"; 
  
echo number_format($num)."\n\n"; 
  
echo number_format($num, 3); 
   
?>
Copy after login

Output:

PHP Warning:  number_format() expects parameter 1 to be float,
string given in /home/ac476aaecea758334cb8ed146bcbb8f6.php on line 5

PHP Warning:  number_format() expects parameter 1 to be float, 
string given in /home/ac476aaecea758334cb8ed146bcbb8f6.php on line 8
Copy after login

number_format() code example 3:

This function does not accept three parameters, only 1, 2 or 4 parameters.

<?php 
$num = 1000000; 
  
echo number_format($num, 3, ", "); 
?>
Copy after login

Output:

PHP Warning:  Wrong parameter count for number_format() 
in /home/e426108b066d9a86366249bf7b626d19.php on line 6
Copy after login

Related recommendations: "PHP Tutorial"

The above is the detailed content of How to use php number_format function?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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