Php number_format 是 Php 中的一个函数,用于格式化给定的千位数字。它是 Php 中的内置函数,根据程序员在参数中给出的具体要求返回格式化的数字,否则在失败时会向用户发出 E_WARNING。可以用逗号或其他分隔符分隔千位。它接受浮点数并返回以千位分隔的格式化数字的字符串,该数字具有四舍五入值或小数值到特定数字。默认情况下,number_format() 函数在向其中传递数字时使用 (,) 运算符分隔一千组。它是表示浮点数的简单且用户友好的方式之一,用户很容易理解。
开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
下面给出了在 Php 中使用 number_format() 函数的基本语法:
string number_format( float $num, int $decimal =0, string $dec_pt = ' . ' , string $thousand_sep = ' , ' )
哪里,
下面给出了描述 Php 中 number_format() 函数工作原理的一些要点:
程序中描述 Php number_format() 函数工作原理的一些示例如下:
代码:
<!DOCTYPE html> <html> <body> <?php $f_num = "Number"; //Using only 1 parameter, i.e. number to be formatted echo " The formatted number is ". number_format($f_num)."\n\n"; //Using the decimal point and printing the decimal upto the specific digits (2 parameters) echo number_format($f_num, 5); ?> </body> </html>
输出:
解释:在上面的代码中,变量‘f_num’中的数字应该是浮点数,但却是一个字符串。因此基本上,字符串作为 number_format() 函数中的参数传递,这在技术上是不正确的。因此,在控制台上会向用户显示语法错误的错误消息。
代码:
<!DOCTYPE html> <html> <body> <?php $f_num = "123.78"; $f_num1 = "67.09"; // With only 1 parameter , i.e. number to be formatted echo number_format($f_num).'<br>'; echo number_format($f_num1).'<br>'; // Using the decimal point (2 parameters) echo number_format($f_num, 5).'<br>'; echo number_format($f_num1, 3); ?> </body> </html>
输出:
解释:在上面的代码中,使用变量“f_num”和“f_num1”给出了两个浮点数。仅使用数字变量作为 number_format() 函数中的参数,将分离一千个组。由于两个给定的数字中都不存在一千个组,因此不存在分离。为了将值打印到特定的小数点,需要传递值为 5 和 3 的第二个参数。因此,在输出中,小数点到给定位数的浮点数将打印在控制台上。
代码:
<!DOCTYPE html> <html> <body> <?php $f_num = "178923.78"; $f_num1 = "665467.09"; // Using only 1 parameter, i.e. number to be formatted echo number_format($f_num).'<br>'; echo number_format($f_num1).'<br>'; // Using the separators for decimal point and thousand groups(4 parameters) echo number_format($f_num, 5, '.', ',').'<br>'; echo number_format($f_num1, 3, ':', '.'); ?> </body> </html>
输出:
Explanation: In the above code, a single parameter (number to be formatted) is passed in the function number_format() and a thousand groups in the number given are separated with the (,) operator by default. In order to print the number with the specific separators in thousand groups and decimal points, third and fourth parameters are passed using those separators in the function and printed on the console.
Code:
<!DOCTYPE html> <html> <body> <?php $f_num = "178923.78"; $f_num1 = "665467.09"; //Using the separators for decimal point only(3 parameters) echo number_format($f_num, 5, ",").'<br>'; echo number_format($f_num1, 3, "."); ?> </body> </html>
Output:
Explanation: In the above code, 2 floating numbers are given and only 3 parameters are passed in the number_format() function which is not allowed technically. As the number_format() function either accepts 1, 2 or 4 parameters. So a Warning message is displayed to the user indicating the wrong parameters.
The above description clearly explains what is the number_format() function in Php and how it works to format the floating numbers. This function provides the flexibility to the user to display the output according to the specific requirements by passing the different parameters. It also shows warnings and results in unexpected outputs. So one needs to understand it properly and needs to be very careful before using it in a program.
以上是PHP number_format()的详细内容。更多信息请关注PHP中文网其他相关文章!