PHP: Hypertext Preprocessor (original) (raw)

number_format

(PHP 4, PHP 5, PHP 7, PHP 8)

number_format — Format a number with grouped thousands

Parameters

num

The number being formatted.

decimals

Sets the number of decimal digits. If 0, the decimal_separator is omitted from the return value. As of PHP 8.3.0, when the value is negative, num is rounded to decimals significant digits before the decimal point. Prior to PHP 8.3.0, negative values were ignored and handled the same as 0.

decimal_separator

Sets the separator for the decimal point.

thousands_separator

Sets the thousands separator.

Return Values

A formatted version of num.

Changelog

Version Description
8.3.0 Added handling of negative values for decimals.
8.0.0 Prior to this version, number_format() accepted one, two, or four parameters (but not three).
7.2.0 number_format() was changed to not being able to return -0, previously -0 could be returned for cases like where num would be -0.01.

Examples

Example #1 number_format() Example

For instance, French notation usually use two decimals, comma (',') as decimal separator, and space (' ') as thousand separator. The following example demonstrates various ways to format a number:

`<?php

$number

= 1234.56;// english notation (default)
echo number_format($number), PHP_EOL;
// 1,235

// French notation

echo number_format($number, 2, ',', ' '), PHP_EOL;
// 1 234,56$number = 1234.5678;// english notation without thousands separator
echo number_format($number, 2, '.', ''), PHP_EOL;
// 1234.57?>`

Example #2 A negative value for decimals

As of PHP 8.3.0, a negative value for decimals is used to round the number of significant digits before the decimal point.

<?php $number = "1234.5678"; var_dump(number_format($number, -1)); var_dump(number_format($number, -2)); var_dump(number_format($number, -3)); ?>

The above example will output:

string(5) "1,230" string(5) "1,200" string(5) "1,000"

See Also

Found A Problem?

thomas at weblizards dot de

16 years ago

`It's not explicitly documented; number_format also rounds:

number."−>".numberformat(number."->".number_format(number.">".numberformat(number, 2, '.', ',')."
"; ?>

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

`

info at ensostudio dot ru

3 years ago

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); // двенадцать тысяч триста семь целых пять десятых ?>

james at bandit dot co.nz

16 years ago

`Outputs a human readable number.

n=(0+strreplace(",","",n = (0+str_replace(",","",n=(0+strreplace(",","",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

`

Jeroen de Bruijn [NL]

19 years ago

`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(′[.,]′,decimals) = split ('[.,]', decimals)=split([.,],value, 2);
if (intval($decimals) > 0)
return number_format($value,2,".",",");
else
return number_format($value,0,".",",") .",-";
}

`

Theo Diem

22 years ago

`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

`

MarcM

19 years ago

`For Zero fill - just use the sprintf() function

$pr_id = 1; prid=sprintf("pr_id = sprintf("%03d", prid=sprintf("pr_id);
echo $pr_id;

//outputs 001

$pr_id = 10; prid=sprintf("pr_id = sprintf("%03d", prid=sprintf("pr_id);
echo $pr_id;

//outputs 010

You can change %03d to %04d, etc.

`

stm555 at hotmail dot com

20 years ago

`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.

brokennumber=explode(broken_number = explode(brokennumber=explode(decimal,$number); return number_format($broken_number[0]).$decimal.$broken_number[1]; } ?>

`