最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
PHP中number_format函数输出数字格式化,增加千分位符号
时间:2022-06-24 19:50:40 编辑:袖梨 来源:一聚教程网
在输出数据到屏幕上显示的时候,如果数据较大,位数较多,看上去会比较费劲,有一种比较直观的方法是使用千分位,也就是每三位数字显示一个逗号,这样可以快速的知道数的大小,不用一位位的去慢慢数了。
令人高兴的是,php中有专门的函数可以完成这个任务,可以在输出数据的时候自动加上千分位。
string number_format ( float number [, int decimals [, string dec_point, string thousands_sep]] )
number_format有四个参数,第一个参数是要输出的数字(浮点类型),这个参数是必需的,后面三个参数为可选的,其中后面两个参数要么全没有,要么全提供
number 必需。要格式化的数字。如果未设置其他参数,则数字会被格式化为不带小数点且以逗号 (,) 作为分隔符。
decimals 可选。规定多少个小数。如果设置了该参数,则使用点号 (.) 作为小数点来格式化数字。
decimalpoint 可选。规定用作小数点的字符串。
separator 可选。规定用作千位分隔符的字符串。仅使用该参数的第一个字符。比如 “xyz” 仅输出 “x”。
string number_format(
float number, //要输出的数字
int decimals, //小数位的位数,默认为0
string dec_point, //小数点的表示,默认为.
string thousands_sep //千分位的表示,默认为,
)
下面搞个例子试试
echo number_format('1234.56');
echo number_format('1234.56',1);
echo number_format('1234.56',2);
echo number_format('1234.56',3);
echo number_format('1234.56',2,'-','/');
结果如下
1,235 // 四舍五入
1,234.6 //
1,234.56 //
1,234.560 // 小数位不足,补充0
1/234-56 // 千分位符号变成/,小数点符号变为-
例子
number_format
$number = 1234.56;
// english notation (default)
$english_format_number = number_format($number);
// 1,235
// French notation
$nombre_format_francais = number_format($number, 2, ',', ' ');
// 1 234,56
$number = 1234.5678;
// english notation without thousands seperator
$english_format_number = number_format($number, 2, '.', '');
// 1234.57
?>
相关文章
- 英雄联盟手游芮尔怎么连招-熔铁少女连招技巧 09-18
- 禁闭求生2怎么做蝴蝶长袍 蝴蝶长袍制作方法 09-18
- 地狱即我们无人机怎么升级 模块安装与技能解锁指南 09-18
- 无主之地4圣城终点区玉米卷饼在哪 圣城终点区玉米卷饼收集攻略 09-18
- 禁闭求生2蝴蝶头箍怎么做 蝴蝶头箍制作攻略 09-18
- 我的休闲时光1月优家风尚新衣服有哪些-1月优家风尚新增服饰一览 09-18