number_format() - 以千位分隔符方式格式化一个数字 - php 字符串函数
number_format()
(PHP 4, PHP 5, PHP 7)
以千位分隔符方式格式化一个数字
说明
number_format( float $number[,int $decimals= 0] ): stringnumber_format( float $number, int $decimals= 0, string $dec_point= ".", string $thousands_sep= ",") : string本函数可以接受1个、2个或者4个参数(注意:不能是3个):
如果只提供第一个参数,$number的小数部分会被去掉并且每个千位分隔符都是英文小写逗号","
如果提供两个参数,$number将保留小数点后的位数到你设定的值,其余同楼上
如果提供了四个参数,$number将保留$decimals个长度的小数部分,小数点被替换为$dec_point,千位分隔符替换为$thousands_sep
参数
$number你要格式化的数字
$decimals要保留的小数位数
$dec_point指定小数点显示的字符
$thousands_sep指定千位分隔符显示的字符
返回值
格式化以后的$number.
更新日志
版本 | 说明 |
---|---|
5.4.0 | This function now supports multiple bytes in$dec_pointand$thousands_sep. Only the first byte of each separator was used in older versions. |
范例
Example #1number_format()Example
For instance, French notation usually use two decimals, comma (',') as decimal separator, and space ('') as thousand separator. The following example demonstrates various way to format a number:
更新日志
版本 | 说明 |
---|---|
7.2.0 | number_format()现在无法返回-0,之前可能返回-0,因为$number可能会是-0.01。 |
参见
money_format()
将数字格式化成货币字符串sprintf()
Return a formatted stringprintf()
输出格式化字符串sscanf()
根据指定格式解析输入的字符
It's not explicitly documented; number_format also rounds: 0.001->0.00 0.002->0.00 0.003->0.00 0.004->0.00 0.005->0.01 0.006->0.01 0.007->0.01 0.008->0.01 0.009->0.01
Outputs a human readable number. Outputs: 247,704,360 -> 247.7 million 866,965,260,000 -> 867 billion
I ran across an issue where I wanted to keep the entered precision of a real value, without arbitrarily rounding off what the user had submitted. I figured it out with a quick explode on the number before formatting. I could then format either side of the decimal.
For Zero fill - just use the sprintf() function $pr_id = 1; $pr_id = sprintf("%03d", $pr_id); echo $pr_id; //outputs 001 ----------------- $pr_id = 10; $pr_id = sprintf("%03d", $pr_id); echo $pr_id; //outputs 010 ----------------- You can change %03d to %04d, etc.
Simple function to show money as only dollars if no cents, but will show 2 decimals if cents exist. The 'cents' flag can force to never or always show 2 decimals Cheers :) And remember to always contribute custom functions if they might be useful to the rest of us or future versions of the php language.
// Here is a function that produces the same output as number_format() but also works with numbers bigger than 2^53. function a_number_format($number_in_iso_format, $no_of_decimals=3, $decimals_separator='.', $thousands_separator='', $digits_grouping=3){ // Check input variables if (!is_numeric($number_in_iso_format)){ error_log("Warning! Wrong parameter type supplied in my_number_format() function. Parameter \$number_in_iso_format is not a number."); return false; } if (!is_numeric($no_of_decimals)){ error_log("Warning! Wrong parameter type supplied in my_number_format() function. Parameter \$no_of_decimals is not a number."); return false; } if (!is_numeric($digits_grouping)){ error_log("Warning! Wrong parameter type supplied in my_number_format() function. Parameter \$digits_grouping is not a number."); return false; } // Prepare variables $no_of_decimals = $no_of_decimals * 1; // Explode the string received after DOT sign (this is the ISO separator of decimals) $aux = explode(".", $number_in_iso_format); // Extract decimal and integer parts $integer_part = $aux[0]; $decimal_part = isset($aux[1]) ? $aux[1] : ''; // Adjust decimal part (increase it, or minimize it) if ($no_of_decimals > 0){ // Check actual size of decimal_part // If its length is smaller than number of decimals, add trailing zeros, otherwise round it if (strlen($decimal_part) 0){ $aux = strrev($integer_part); $integer_part = ''; for ($i=strlen($aux)-1; $i >= 0 ; $i--){ if ( $i % $digits_grouping == 0 && $i != 0){ $integer_part .= "{$aux[$i]}{$thousands_separator}"; } else { $integer_part .= $aux[$i]; } } } $processed_number = "{$integer_part}{$decimals_separator}{$decimal_part}"; return $processed_number; } $original_number= 9223372036854775805; echo a_number_format($original_number, 4, '.',"'",3); // Outputs: 9'223'372'036'854'775'805.1230
I'd like to comment to the old notes of "stm555" and "woodynadobhar". They wrote about "number_format_unlimited_precision()". I guess many of us need that kind of function, which is the almost same function as number_format but don't round a number. Does Anyone know any new solution in a recent PHP version? ... If no, how about the following function? (I fixed somethings like bugs of the function in the old comment.)
I was looking for a SIMPLE way to format currency and account for negative values while not losing the calculation properties of my number. Here's my function - it's not rocket science, but maybe can help someone along the way. Sample use: Returns (for example) $ 44,561.00 or, if a negative ($ 407,250.00) This way, I use my 1st variable for calculations and my 2nd variable for output. I'm sure there are better ways to do it, but this got me back on track.
Exemplo: Example:
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!