
本篇文章主要给大家介绍php number_format用法,希望对需要的朋友有所帮助!
number_format()函数是PHP中的一个内置函数,用于格式化一个包含数千个分组的数字。它在成功时返回格式化的数字,否则在失败时给出E_WARNING。
语法:
string number_format ( $number, $decimals, $decimalpoint, $sep )
参数:
该函数接受上述四个参数,如下所述:
$number:它是指定要格式化的数字的必需参数。number_format php如果没有设置其他参数,则该数字将被格式化为不带小数,并使用逗号(,)作为千位分隔符。必须参数指定要格式化的数字。number_format php

分享一个javascript脚本代码,用于验证只能输入数字和一个小数点,检测数字输入是否符合要求,效果不错,有用到的朋友拿去吧。用于无穷大值、数字、千位分隔符、小数分隔符等的前缀、后缀和各种符号可设置为任意值,并且能在格式化期间正确显示。
$decimalpoint:它是一个可选参数,用于指定要用于小数点的字符串。
$sep:它是可选参数,用于指定用于千位分隔符的字符串。如果给定了该参数,则需要所有其他参数。
返回值:
成功时返回格式化的数字,失败时返回E_WARNING。
number_format()代码示例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");
?>

输出:
999,999 999,999.490 10,000,000 9,999,999.990 1GGG000GGG000#990
number_format()代码示例2:
如果传递任何东西而不是数字,它则会给出警告。
<?php $num = "GFG"; echo number_format($num)."\n\n"; echo number_format($num, 3); ?>
输出:
PHP Warning: number_format() expects parameter 1 to be float, string given in /home/a76aaecea758334cb8ed146bcbb8f6.php on line 5 PHP Warning: number_format() expects parameter 1 to be float, string given in /home/a76aaecea758334cb8ed146bcbb8f6.php on line 8
number_format()代码示例3:
此函数不接受三个参数,只接受1、2或4个参数。
本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/jisuanjixue/article-93962-1.html
我估计很多还是雇佣的
措辞得体
雷达探测精度和灵敏度
这个世界带给我们很多的新生命