PHP中数字千分位逗号分隔函数number_format()

number_format

摘自PHP官方文档

(PHP 4, PHP 5)

number_format 以千位分隔符方式格式化一个数字

说明

string number_format ( float$number [, int$decimals = 0 ] )
string number_format ( float$number , int$decimals = 0 , string $dec_point = '.' , string $thousands_sep = ',' )

本函数可以接受1个、2个或者4个参数(注意:不能是3个):

如果只提供第一个参数, number 的小数部分会被去掉 并且每个千位分隔符都是英文小写逗号","

如果提供两个参数, number 将保留小数点后的位数到你设定的值,其余同楼上

如果提供了四个参数, number 将保留decimals 个长度的小数部分, 小数点被替换为 dec_point ,千位分隔符替换为 thousands_sep

参数

number

你要格式化的数字

decimals

要保留的小数位数

dec_point

指定小数点显示的字符

thousands_sep

指定千位分隔符显示的字符      


Example #1 number_format() Example

For instance, French notation usually use two decimals, comma (',') as decimal separator, and space (' ') as thousand separator. This is achieved with this line :

<?php

$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 separator
$english_format_number =number_format ( $number , 2 , '.' , '' );
// 1234.57

?>

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值