number_format
(PHP 4, PHP 5)
number_format — Format a number with grouped thousands
Description
$number
[, int $decimals
= 0
] )$number
, int $decimals
= 0
, string $dec_point
= "."
, string $thousands_sep
= ","
)This function accepts either one, two, or four parameters (not three):
If only one parameter is given,
number
will be formatted without decimals,
but with a comma (",") between every group of thousands.
If two parameters are given, number
will
be formatted with decimals
decimals with a
dot (".") in front, and a comma (",") between every group of
thousands.
If all four parameters are given, number
will be formatted with decimals
decimals,
dec_point
instead of a dot (".") before
the decimals and thousands_sep
instead of
a comma (",") between every group of thousands.
Parameters
-
number
-
The number being formatted.
-
decimals
-
Sets the number of decimal points.
-
dec_point
-
Sets the separator for the decimal point.
-
thousands_sep
-
Sets the thousands separator.
Return Values
A formatted version of number
.
Changelog
Version | Description |
---|---|
5.4.0 |
This function now supports multiple bytes in
dec_point and
thousands_sep . Only the first byte of each
separator was used in older versions.
|
Examples
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
?>
See Also
- money_format() - Formats a number as a currency string
- sprintf() - Return a formatted string
- printf() - Output a formatted string
- sscanf() - Parses input from a string according to a format
- addcslashes
- addslashes
- bin2hex
- chop
- chr
- chunk_split
- convert_cyr_string
- convert_uudecode
- convert_uuencode
- count_chars
- crc32
- crypt
- echo
- explode
- fprintf
- get_html_translation_table
- hebrev
- hebrevc
- hex2bin
- html_entity_decode
- htmlentities
- htmlspecialchars_decode
- htmlspecialchars
- implode
- join
- lcfirst
- levenshtein
- localeconv
- ltrim
- md5_file
- md5
- metaphone
- money_format
- nl_langinfo
- nl2br
- number_format
- ord
- parse_str
- printf
- quoted_printable_decode
- quoted_printable_encode
- quotemeta
- rtrim
- setlocale
- sha1_file
- sha1
- similar_text
- soundex
- sprintf
- sscanf
- str_getcsv
- str_ireplace
- str_pad
- str_repeat
- str_replace
- str_rot13
- str_shuffle
- str_split
- str_word_count
- strcasecmp
- strchr
- strcmp
- strcoll
- strcspn
- strip_tags
- stripcslashes
- stripos
- stripslashes
- stristr
- strlen
- strnatcasecmp
- strnatcmp
- strncasecmp
- strncmp
- strpbrk
- strpos
- strrchr
- strrev
- strripos
- strrpos
- strspn
- strstr
- strtok
- strtolower
- strtoupper
- strtr
- substr_compare
- substr_count
- substr_replace
- substr
- trim
- ucfirst
- ucwords
- vfprintf
- vprintf
- vsprintf
- wordwrap
Коментарии
formatting numbers may be more easy if u use number_format function.
I also wrote this :
function something($number)
{
$locale = localeconv();
return number_format($number,
$locale['frac_digits'],
$locale['decimal_point'],
$locale['thousands_sep']);
}
hope this helps =)
[]'s
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.
<?php
function number_format_unlimited_precision($number,$decimal = '.')
{
$broken_number = explode($decimal,$number);
return number_format($broken_number[0]).$decimal.$broken_number[1];
}
?>
If you want to display a number ending with ,- (like 200,-) when there are no decimal characters and display the decimals when there are decimal characters i use:
function DisplayDouble($value)
{
list($whole, $decimals) = split ('[.,]', $value, 2);
if (intval($decimals) > 0)
return number_format($value,2,".",",");
else
return number_format($value,0,".",",") .",-";
}
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.
It's not explicitly documented; number_format also rounds:
<?php
$numbers = array(0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007, 0.008, 0.009);
foreach ($numbers as $number)
print $number."->".number_format($number, 2, '.', ',')."<br>";
?>
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.
<?php
# Output easy-to-read numbers
# by james at bandit.co.nz
function bd_nice_number($n) {
// first strip any formatting;
$n = (0+str_replace(",","",$n));
// is this a number?
if(!is_numeric($n)) return false;
// now filter it;
if($n>1000000000000) return round(($n/1000000000000),1).' trillion';
else if($n>1000000000) return round(($n/1000000000),1).' billion';
else if($n>1000000) return round(($n/1000000),1).' million';
else if($n>1000) return round(($n/1000),1).' thousand';
return number_format($n);
}
?>
Outputs:
247,704,360 -> 247.7 million
866,965,260,000 -> 867 billion
Note: use NumberFormatter to convert in human-readable format instead user function from comments:
<?php
echo NumberFormatter::create('en', NumberFormatter::SPELLOUT)->format(12309); // twelve thousand three hundred nine
echo NumberFormatter::create('ru', NumberFormatter::SPELLOUT)->format(12307.5); // двенадцать тысяч триста семь целых пять десятых
?>