PHP
downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

ord> <nl2br
Last updated: Tue, 30 Jun 2009

view this page in

number_format

(PHP 4, PHP 5)

number_formatFormatuje liczbę według szablonu

Opis

string number_format ( float $liczba [, int $miejsc_dziesiętnych [, string $przecinek_dziesiętny ]], string $separator_tysięcy )

Funkcja number_format() zwraca sformatowaną liczbę liczba według podanych argumentów. Ta funkcja przyjmuje jeden, dwa lub cztery argumenty (nie trzy):

Jeśli podany jest tylko jeden argument, liczba zostanie sformatowana bez miejsc dziesiętnych ale z przecinkiem (",") pomiędzy każdą grupą tysięcy.

Jeśli podane są dwa argumenty, liczba będzie sformatowana z miejsc_dziesiętnych , z kropką (".") w charakterze przecinka dziesiętnego i przecinkiem (",") pomiędzy każdą grupą tysięcy.

Kiedy wszystkie cztery parametry są podane, liczba będzie sformatowana z miejsc_dziesiętnych , z przecinek_dziesiętny zamiast kropki (".") i separator_tysięcy pomiędzy każdą grupą tysięcy.

Informacja: Tylko pierwszy znak z separator_tysięcy jest wykorzystywany. Na przykład, jeśli użyjesz foo jako separatora, to z liczby 1000 funkcja zwróci 1f000.

Przykład #1 number_format() - przykłady

W polskiej notacji liczbowej używa się przecinka (",") jako przecinka dziesiętnego i spacji (" ") jako separatora tysięcy. W PHP można to zrobić tak:

<?php

    $liczba 
1234.56;

    
// notacja angielska (domyślna)
    
$notacja_angielska number_format($liczba);
    
// 1,234.56

    // notacja polska
    
$notacja_polska number_format($liczba2','' ');
    
// 1 234,56

    
$number 1234.5678;

    
// notacja angielska bez separatora tysięcy
    
$notacja_angielska number_format($liczba2'.''');
    
// 1234.57

?>

Patrz także: money_format(), sprintf(), printf() i sscanf().



ord> <nl2br
Last updated: Tue, 30 Jun 2009
 
add a note add a note User Contributed Notes
number_format
info at daniel-marschall dot de
22-May-2009 07:54
In my function my_number_format() [shown below] there was a bug.

If a negative number which is smaller than 1 was entered (-0,...), then the result was wrongly positive because +0 is equal to -0 (the content of $tmp[0] which was interpretet as numeric value).

Here is the corrected version:

<?php

function my_number_format($number, $dec_point, $thousands_sep)
{
   
$was_neg = $number < 0; // Because +0 == -0
   
$number = abs($number);

   
$tmp = explode('.', $number);
   
$out = number_format($tmp[0], 0, $dec_point, $thousands_sep);
    if (isset(
$tmp[1])) $out .= $dec_point.$tmp[1];

    if (
$was_neg) $out = "-$out";

    return
$out;
}

?>

Thanks to Federico Cassinelli for the bug report.



[EDIT BY danbrown AT php DOT net: The original note follows.]

Let's say we got the number $inp = 1234.56

By using

<?php
return number_format($inp, 2, ',', '.');
?>

you can get the German format 1.234,56. (Comma as decimal separator and point as thousand separator)

But I have a problem with that: I want to add commas as thousand separators and change the decimal-separator (this could also be done with str_replace), but I do not want to change the amount of fractional digits!

But since the 2nd argument of number_format is necessary to enter the 3rd and 4th argument, this cannot be done with number_format. You have to change the fractional digits with this function.

But I want that 1234.56 changes into 1.234,56 and 1234.567890123456 changes into 1.234,567890123456

So, I created following function, that doesn't change the amount of fractional digits:

<?php
function my_number_format($number, $dec_point, $thousands_sep)
{
 
$tmp = explode('.', $number);
 
$out = number_format($tmp[0], 0, $dec_point, $thousands_sep);
  if (isset(
$tmp[1])) $out .= $dec_point.$tmp[1];

  return
$out;
}
?>
james at bandit dot co.nz
27-Mar-2009 05:03
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
IMSoP
18-Mar-2009 10:36
I'm not sure if this is the right place anyway, but "ben at last dot fm"'s ordinal function can be simplified further by removing the redundant "floor" (the result of floor is still a float, it's the "%" that's converting to int) and outer switch.

Note that this version also returns the number with the suffix on the end, not just the suffix.

<?php
function ordinal($num)
{
   
// Special case "teenth"
   
if ( ($num / 10) % 10 != 1 )
    {
       
// Handle 1st, 2nd, 3rd
       
switch( $num % 10 )
        {
            case
1: return $num . 'st';
            case
2: return $num . 'nd';
            case
3: return $num . 'rd'
        }
    }
   
// Everything else is "nth"
   
return $num . 'th';
}
?>
ben at last dot fm
27-Feb-2009 12:22
Writing a function to get English ordinals for numbers is a problem every computer science student will face. Here's my solution - nice, short and simple:

<?php
   
function st($i) {
        switch(
floor($i/10) % 10 ) {
            default:
                switch(
$i % 10 ) {
                    case
1: return 'st';
                    case
2: return 'nd';
                    case
3: return 'rd';  
                }
            case
1:
        }
        return
'th';
    }
?>
Barbara
26-Jan-2009 07:04
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.

<?php
function wims_currency($number) {
   if (
$number < 0) {
    
$print_number = "($ " . str_replace('-', '', number_format ($number, 2, ".", ",")) . ")";
    } else {
    
$print_number = "$ " number_format ($number, 2, ".", ",") ;
   }
   return
$print_number;
}
?>
Sample use:

<?php
$pur_po_total
=  ($pur_po_total + $pur_item_total);
$print_pur_po_total = wims_currency($pur_po_total);
?>

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.
thomas at weblizards dot de
23-Jan-2009 01:43
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
nospam at nospam dot com
02-Dec-2008 10:08
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

<?php

// formats money to a whole number or with 2 decimals; includes a dollar sign in front
function formatMoney($number, $cents = 1) { // cents: 0=never, 1=if needed, 2=always
 
if (is_numeric($number)) { // a number
   
if (!$number) { // zero
     
$money = ($cents == 2 ? '0.00' : '0'); // output zero
   
} else { // value
     
if (floor($number) == $number) { // whole number
       
$money = number_format($number, ($cents == 2 ? 2 : 0)); // format
     
} else { // cents
       
$money = number_format(round($number, 2), ($cents == 0 ? 0 : 2)); // format
     
} // integer or decimal
   
} // value
   
return '$'.$money;
  }
// numeric
} // formatMoney

$a = array(1, 1234, 1.5, 1.234, 2.345, 2.001, 2.100, '1.000', '1.2345', '12345', 0, '0.00');

// show cents if needed ($cents=1)
foreach ($a as $b) echo ('<br />'.$b.' = '.formatMoney($b, 1));
1 = $1
1234
= $1,234
1.5
= $1.50
1.234
= $1.23
2.345
= $2.35
2.001
= $2.00
2.1
= $2.10
1.000
= $1
1.2345
= $1.23
12345
= $12,345
0
= $0
0.00
= $0

// never show cents ($cents=0)
foreach ($a as $b) echo ('<br />'.$b.' = '.formatMoney($b, 0));
1 = $1
1234
= $1,234
1.5
= $2
1.234
= $1
2.345
= $2
2.001
= $2
2.1
= $2
1.000
= $1
1.2345
= $1
12345
= $12,345
0
= $0
0.00
= $0

// always show cents ($cents=2)
foreach ($a as $b) echo ('<br />'.$b.' = '.formatMoney($b, 2));
1 = $1.00
1234
= $1,234.00
1.5
= $1.50
1.234
= $1.23
2.345
= $2.35
2.001
= $2.00
2.1
= $2.10
1.000
= $1.00
1.2345
= $1.23
12345
= $12,345.00
0
= $0.00
0.00
= $0.00

?>

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.
samuelpeixoto at gmail dot com
21-Nov-2008 01:05
Exemplo: Example:

<?php
$number
= 1234567.896;
echo
'1: '.number_format($number, 2, ',', '').'<br>';
echo
'2: '.number_format($number, 2, '.', '').'<br>';
echo
'3: '.number_format($number, 2, ',', '.').'<br>';
echo
'4: '.number_format($number, 2, '.', ',').'<br>';
echo
'5: '.number_format($number, 2, ',', ' ').'<br>';
echo
'6: '.number_format($number, 2, ',', "'").'<br>';
echo
'7: '.number_format($number, 2, '', '').'<br>';
?>

Resultado: Result:

 1: 1234567,90   -> Decimal separado por ,
 2: 1234567.90   -> Decimal separado por .
 3: 1.234.567,90 -> Moeda Brasil, Alemanha
 4: 1,234,567.90 -> Inglês, USA
 5: 1 234 567,90 -> França
 6: 1'234'567,90 -> Suíça
 7: 123456790    -> Sem decimal
with at held dot com
16-Sep-2008 06:17
Note:
The accuracy tends to drift if you're setting a large decimal value.

<?php
$number
= 1234.560;
echo
number_format($number, 2, '.', '');
echo
number_format($number, 3, '.', '');
echo
number_format($number, 4, '.', '');
echo
number_format($number, 10, '.', '');
echo
number_format($number, 20, '.', '');
echo
number_format($number, 50, '.', '');
?>

echos this (for me at least).

1234.56
1234.560
1234.5600
1234.5600000000
1234.55999999999994543032
1234.55999999999994543031789362430572509765625000000000

SyCo
isapoetra at gmail dot com
15-Jun-2008 01:53
here is the code to convert number to Indonesian text, this code has limitation as is number_format function. sorry for this.
/*
* Created : Iwan Sapoetra - Jun 13, 2008
* Project : Web
* Package : cgaf
*
*/
function terbilang( $num ,$dec=4){
    $stext = array(
        "Nol",
        "Satu",
        "Dua",
        "Tiga",
        "Empat",
        "Lima",
        "Enam",
        "Tujuh",
        "Delapan",
        "Sembilan",
        "Sepuluh",
        "Sebelas"
    );
    $say  = array(
        "Ribu",
        "Juta",
        "Milyar",
        "Triliun",
        "Biliun", // remember limitation of float
        "--apaan---" ///setelah biliun namanya apa?
    );
    $w = "";

    if ($num <0 ) {
        $w  = "Minus ";
        //make positive
        $num *= -1;
    }

    $snum = number_format($num,$dec,",",".");
    die($snum);
    $strnum =  explode(".",substr($snum,0,strrpos($snum,",")));
    //parse decimalnya
    $koma = substr($snum,strrpos($snum,",")+1);

    $isone = substr($num,0,1)  ==1;
    if (count($strnum)==1) {
        $num = $strnum[0];
        switch (strlen($num)) {
            case 1:
            case 2:
                if (!isset($stext[$strnum[0]])){
                    if($num<19){
                        $w .=$stext[substr($num,1)]." Belas";
                    }else{
                        $w .= $stext[substr($num,0,1)]." Puluh ".
                            (intval(substr($num,1))==0 ? "" : $stext[substr($num,1)]);
                    }
                }else{
                    $w .= $stext[$strnum[0]];
                }
                break;
            case 3:
                $w .=  ($isone ? "Seratus" : terbilang(substr($num,0,1)) .
                    " Ratus").
                    " ".(intval(substr($num,1))==0 ? "" : terbilang(substr($num,1)));
                break;
            case 4:
                $w .=  ($isone ? "Seribu" : terbilang(substr($num,0,1)) .
                    " Ribu").
                    " ".(intval(substr($num,1))==0 ? "" : terbilang(substr($num,1)));
                break;
            default:
                break;
        }
    }else{
        $text = $say[count($strnum)-2];
        $w = ($isone && strlen($strnum[0])==1 && count($strnum) <=3? "Se".strtolower($text) : terbilang($strnum[0]).' '.$text);
        array_shift($strnum);
        $i =count($strnum)-2;
        foreach ($strnum as $k=>$v) {
            if (intval($v)) {
                $w.= ' '.terbilang($v).' '.($i >=0 ? $say[$i] : "");
            }
            $i--;
        }
    }
    $w = trim($w);
    if ($dec = intval($koma)) {
        $w .= " Koma ". terbilang($koma);
    }
    return trim($w);
}
//example
echo terbilang(999999999999)."\n";
/**
 * result : Sembilan Ratus Sembilan Puluh Sembilan Milyar Sembilan Ratus Sembilan Puluh Sembilan Juta Sembilan Ratus Sembilan Puluh Sembilan Ribu Sembilan Ratus Sembilan Puluh Sembilan
 */
echo terbilang(9999999999999999);
/**
 * todo : fix this bug pleasese
 * problem : number_format(9999999999999999) <--- 10.000.000.000.000.000,0000
 * Result : Sepuluh Biliun
 */
xkeeper at gmail dot com
10-Jun-2008 05:26
Technically, you can just use number_format() with "x" as the thousands seperator, then...

<?php
  str_replace
("x", "character of choice", number_format(1234.56, ".", "x"));
?>

This will probably be many times more efficient.
team at glossword dot biz
26-May-2008 01:45
It's pity that the function supports one-byte characters only for "thousands separator".
For English it works well but for French and Russian it makes problem.
Developers should use character &#160; or chr(0xA0) as "thousands separator" to avoid the number of being moved to a new line in HTML-documents. But number_format() didn't understand that symbol.
To solve the problem, we wrote our own variant of number_format():

<?php
/* Replacement for number_format() */
function my_number_format($n, $decimals = 0, $dec_point = ',', $thousands_sep = '&#160;')
{
   
$b = explode('.', $n);
   
$rn = '';
   
$l = strlen($b[0]);
   
/* Reverse string */
   
for ($i = $l; $i > 3; $i -= 3)
    {
       
$rn = $thousands_sep . substr($b[0], $i - 3, 3) . $rn;
    }
   
/* sprintf() used to correct 0.79 to 0.790 */
    /* str_replace() used to correct decimals */
    /* str_repeat() used to correct decimals */
   
return substr($b[0], 0, $i) . $rn . ($decimals
           
? $dec_point.(isset($b[1])
                ?
str_replace('0.', '', sprintf('%0.'.$decimals.'f', '0.'.$b[1]))
                :
str_repeat(0, $decimals))
            :
'');
}
/* Outputs 123&#160;456,790 */
print my_number_format(123456.78987, 3);
?>

It can be optimized more, but this variant works well also.
Vladimir Kornea
12-May-2008 10:16
In response to gabrielu at gmail dot com concerning number_format(14.95 * 0.3, 2) returning 4.48 rather than 4.49:

What you're seeing is not specific to the number_format() function, but is an intractable consequence of performing floating-point arithmetic. Your computer is unable to create exact representations of certain floating-point numbers and instead approximates. The result of your calculations is stored internally not as 4.485 but as something resembling 4.484999... This is the reason for the advice to never check for equality when dealing with floating-point numbers--while PHP might echo the same thing, the internal representation could be quite different. See the code below.

<?php

$array
= array();
$array['assigned'] = 4.485;
$array['calculated'] = 14.95 * 0.3; // equals 4.485 in reality
echo "Sorting ascending\n"; // notice that 'calculated' gets sorted ahead of 'assigned'
asort($array);
foreach(
$array as $key => $value) {
    echo
"$key: $value becomes " . number_format($value, 2) . "\n";
}
echo
"Sorting descending\n";
arsort($array); // notice that 'assigned' gets sorted ahead of 'calculated'
foreach($array as $key => $value) {
    echo
"$key: $value becomes " . number_format($value, 2) . "\n";

}

?>

Output:

Sorting ascending
calculated: 4.485 becomes 4.48
assigned: 4.485 becomes 4.49
Sorting descending
assigned: 4.485 becomes 4.49
calculated: 4.485 becomes 4.48
gabrielu at gmail dot com
08-May-2008 04:54
Using the number_format I'm having some unexpected results.  30% of 14.95 (14.95 * .3) = 4.485.  Now 4.485 rounded to two decimal places should give me 4.49.

Example:
<?php
echo number_format(14.95 * .3, 2, '.', '') . "\n";
echo
number_format(4.485, 2, '.', '') . "\n";
?>

Unexpected Results:
4.48
4.49
eregon at msn dot com
30-Jan-2008 09:02
I have seen some scripts here for format a filesize, but i think this can be useful:

<?php
function format_filesize($number, $decimals = 3, $force_unit = false, $dec_char = ',', $thousands_char = ' ')
    {
//string format_filesize(int(0,) $number, (bool(0), int(0,4)) $force_unit, int $decimals, char $dec_char, char $thousands_char)
    //format a filesize $number with unit (setted by $force_unit(see below for the number))
       
$units = array('o', 'Ko', 'Mo', 'Go', 'To');
        if(
$force_unit === false)
           
$unit = floor(log($number, 2) / 10);
        else
           
$unit = $force_unit;
        if(
$unit == 0)
           
$decimals = 0;
        return
number_format($number / pow(1024, $unit), $decimals, $dec_char, $thousands_char).' '.$units[$unit];
    }
?>

ex:
format_filesize(2540367) => '2,423 Mo'
format_filesize(2540367, 2) => '2,42 Mo'
format_filesize(2540367, 1, 1) => '2 480,8 Ko' //0=>'o', 1=>'Ko', 2=>'Mo', 3=>'Go', 4=>'To' (see $units)
format_filesize(2540687367, 2, 1, ',', '.') => '2.481.140,01 Ko'

log($number, 2) / 10 = log($number, 1024) ;)
Fast & Easy, isn't it? :)
www.produkte24.com
08-Jan-2008 08:28
I have to verify price data from several formats, eg:
- 12.345,67 (German)
- 12,345.67 (English)
- 12 345,67 (French)
- 12'345,67 (Swiss)

Here is a quick function which helps me out of all this format mess, but please not the I only check for:
- positive values
- max 2 digits on the right side (.12)

function mk_price_to_float($price){
    $price = trim($price);
    if(preg_match("~^([0-9]+|(?:(?:[0-9]{1,3}([.,' ]))+[0-9]{3})+)(([.,])[0-9]{1,2})?$~", $price, $r)){
        if(!empty($r['2'])){
            $pre = preg_replace("~[".$r['2']."]~", "", $r['1']);
        }else{
            $pre = $r['1'];
        }
        if(!empty($r['4'])){
            $post = ".".preg_replace("~[".$r['4']."]~", "", $r['3']);
        }else{
            $post = false;
        }
        $form_price = $pre.$post;
        return $form_price;
    }
    return false;
}

This code is used at http://www.produkte24.com/ and http://www.who-sells-it.com/ and works like a charm.
uliciadrian01 at yahoo dot com
06-Oct-2007 09:34
A simple funtion to format american dollars.
<?
function formatMoney($money) {
    if(
$money<1) {
       
$money='&cent;'.$money*100;
    }
    else {
       
$dollars=intval($money);
       
$cents=$money-$dollars;
       
$cents=$cents*100;
       
$money='$'.$dollars.' and &cent;'.$cents;
    }
    return
$money;
}
echo
formatmoney('52.342');
?>
This will output: "   $52 and ¢34.2  ".
eb1024 at gmail dot com
12-Aug-2007 05:38
To address the problems number_format has when dealing with big numbers I've created my own Number_Format method, it acts the same way as number_format and takes the same arguments but deals with numbers as strings solving the problems above referred.

(The other methods are available at http://www.alixaxel.com/wordpress/2007/05/19/php-math-library/)

function Number_Format($number, $decimal_precision = 0, $decimals_separator = '.', $thousands_separator = ',')
{
if ($this->Is_Negative($number))
{
    $negative = true;

    $number = str_replace('-', '', $number);
}

$number = explode('.', str_replace(' ', '', $number));

$number[0] = str_split(strrev($number[0]), 3);

$total_segments = count($number[0]);

for ($i = 0; $i < $total_segments; $i++)
{
    $number[0][$i] = strrev($number[0][$i]);
}

$number[0] = implode($thousands_separator, array_reverse($number[0]));

if ($negative === true)
{
    $number[0] = '-' . $number[0];
}

if (!empty($number[1]))
{
    $number[1] = $this->Round($number[1], $decimal_precision);
}

$number = implode($decimals_separator, $number);

return $number;
}

I hope this is useful for someone!
Jason Johnson
01-Aug-2007 07:39
Beware of this pitfall:
$value = number_format ($float_val, 2, ".");

This will silently return an empty string and not throw an error, at least with PHP 5.x.

This is the corrected code:
$value = number_format ($float_val, 2, ".", ",");
- or -
$value = number_format ($float_val, 2);

You must specifiy either the decimal point AND thousands separator else neither, otherwise you'll run into this issue.

I just spent over 30min debugging code with this problem!
user at example dot net
16-Jul-2007 02:52
The following function converts a string into Float or Integer while taking the given or locale number format into account.

<?php

   
function strtonumber( $str, $dec_point=null, $thousands_sep=null )
    {
        if(
is_null($dec_point) || is_null($thousands_sep) ) {
           
$locale = localeconv();
            if(
is_null($dec_point) ) {
               
$dec_point = $locale['decimal_point'];
            }
            if(
is_null($thousands_sep) ) {
               
$thousands_sep = $locale['thousands_sep'];
            }
        }
       
$number = (float) str_replace($dec_point, '.', str_replace($thousands_sep, '', $str));
        if(
$number == (int) $number ) {
            return (int)
$number;
        } else {
            return
$number;
        }
    }

?>
alx at inbox dot lv
15-May-2007 09:14
enjoy the PHP!
<?php
function FormatPrice($price) {
   
$price = preg_replace("/[^0-9\.]/", "", str_replace(',','.',$price));
    if (
substr($price,-3,1)=='.') {
       
$sents = '.'.substr($price,-2);
       
$price = substr($price,0,strlen($price)-3);
    } elseif (
substr($price,-2,1)=='.') {
       
$sents = '.'.substr($price,-1);
       
$price = substr($price,0,strlen($price)-2);
    } else {
       
$sents = '.00';
    }
   
$price = preg_replace("/[^0-9]/", "", $price);
    return
number_format($price.$sents,2,'.','');
}
?>
Joeri
13-Feb-2007 03:18
Aj and astrolox, for perfect precision arithmetic, you can use the bcmath functions, which perform math on string representations of numbers, instead of on floats. Obviously this is an order of magnitude slower, but at least you don't lose any precision due to the complexities of IEEE-754.
ted at qtis dot co dot nz
07-Feb-2007 11:38
I use the following to get around the negative zero problem:

function currency_format($amount, $precision = 2, $use_commas = true, $show_currency_symbol = false, $parentheses_for_negative_amounts = false)
{
    /*
    **    An improvement to number_format.  Mainly to get rid of the annoying behaviour of negative zero amounts.   
    */
    $amount = (float) $amount;
    // Get rid of negative zero
    $zero = round(0, $precision);
    if (round($amount, $precision) == $zero) {
        $amount = $zero;
    }
   
    if ($use_commas) {
        if ($parentheses_for_negative_amounts && ($amount < 0)) {
            $amount = '('.number_format(abs($amount), $precision).')';
        }
        else {
            $amount = number_format($amount, $precision);
        }
    }
    else {
        if ($parentheses_for_negative_amounts && ($amount < 0)) {
            $amount = '('.round(abs($amount), $precision).')';
        }
        else {
            $amount = round($amount, $precision);
        }
    }
   
    if ($show_currency_symbol) {
        $amount = '$'.$amount;  // Change this to use the organization's country's symbol in the future
    }
    return $amount;
}
zulisse at email dot it
06-Feb-2007 10:27
simpler function to convert a number in bytes, kilobytes....

<?php

function bytes($a) {
   
$unim = array("B","KB","MB","GB","TB","PB");
   
$c = 0;
    while (
$a>=1024) {
       
$c++;
       
$a = $a/1024;
    }
    return
number_format($a,($c ? 2 : 0),",",".")." ".$unim[$c];
}

?>

you may also add others units over PeraBytes when the hard disks will reach 1024 PB :)
lathem at gibsoncomputer dot net
22-Dec-2006 05:41
I've discovered that the commas which number_format() inserts as a thousands separators causes math functions to break. 

Bit of a newbie thing, but it bit me in the ass.

eg--

<?
$foo
= number_format(1234, 2);
?>

$foo is now equal to 1,234.00

<?
$bar
= 23;
$result = $foo+$bar;
?>

One would think $result == 1,257.00.  However, because number_format added a comma to $foo, the expression will not evaluate correctly. 

Instead, do things in this order:

<?
$foo
=1234;
$bar=23;
$foobar=number_format($foo+$bar, 2);
?>

$foobar is now set to 1,257.00.

Another method, if you're fixing code you've already written and don't want to change a huge amount, is to remove the thousands separator, using the appropriate variable to number_format. 

<?
$foo
= number_format(1234, 2, '.', '');
$bar = number_format(23, 2, '.', '');
$foobar = number_format($foo+$bar, 2);
?>

$foobar will now be set to 1,257.00
webmaster at WWW.ELLESSEWEB.NET
24-Nov-2006 02:51
This is a simple and useful function to convert a byte number in a KB  or MB:

<?
function filesize_format ($bytes) {
 
$bytes=(float)$bytes;
  if (
$bytes<1024){
 
$numero=number_format($bytes, 0, ',', '.')." Byte";
  return
$numero;
  }
  if (
$bytes<1048576){
     
$numero=number_format($bytes/1024, 2, ',', '.')." KByte";
  return
$numero;
  }
  if (
$bytes>=1048576){
     
$numero=number_format($bytes/1048576, 2, ',', '.')." MByte";
  return
$numero;
  }
}
?>
giovanni dot cappellini at gmail dot com
26-Oct-2006 02:31
About the function of j-a-n at gmx dot de: it's useful, but the argument of the function is $number while the logic of the function requires it to be $in.
dwhitaker at dfwairport dot com
13-Sep-2006 02:56
You know sometimes we forget to add the basic cool stuff...

Found this over in the money_format section and felt some newbie is probably looking for it...

$val = "1000000";
echo number_format($val, 0, "", ","); // returns 1,000,000
26-Aug-2006 11:45
Unfortunately, this function is not multibyte-safe. If you want to use the typographically correct separator for thousands in Swiss German and UTF-8 (‘ - Unicode 2019), you'll only get the first byte - e.g. garbage -  since this function strips anything after the first byte, regardless of the charset used.
j-a-n at gmx dot de
10-Aug-2006 05:04
This function formats an decimal number to a String.
But it does'n use an fixed count of decimals but calculates the needed count of decimals.

<?
function formatNumber( $number, $decimals=2, $dec_point=".", $thousands_sep=",") {
   
$nachkomma = abs($in - floor($in));
   
$strnachkomma = number_format($nachkomma , $decimals, ".", "");

    for (
$i = 1; $i <= $decimals; $i++) {
        if (
substr($strnachkomma, ($i * -1), 1) != "0") {
            break;
        }
    }
   
    return
number_format($in, ($decimals - $i +1), $dec_point, $thousands_sep);
}
?>
Example:

formatNumber( 100.00 );
--> 100
formatNumber( 100.50 );
--> 100.5
formatNumber( 100.1234 );
--> 100.12
formatNumber( 100.12, 4 );
--> 100.12
formatNumber( 100.12345, 4 );
--> 100.1234
04-Jun-2006 07:55
The modification to number_format below does what I actually want it to do, with currency, for example. I want it to show $1.40 and not $1.4 or $1.400 - except in the rare case where I have $1.435 for something, in which case I *don't* want it to round. The ugliness below serves my purpose.

function nof($number,$decimals) {
if (number_format($number,$decimals,'.',',') != $number) {
        return $number;
} else {
        return number_format($number,$decimals,'.',',');
        }
}
shpoffo .a.t.. g mail ...
31-May-2006 04:31
For those that need to print the word-form of a number ("twenty-three" instead of "23") I've found the following code to be useful.  This code is an improvement upon that posted in 2004 by armstrong @ rice.  Notably, the previous code did not properly compute large numbers (at least in PHP 4.x that I tested). 

The following code adds another variable to the function, which controls the display of fractional parts, and properly calculates values one thousand and above.

Zak
~ ~ ~

function num2words($num, $c=1) {
    $ZERO = 'zero';
    $MINUS = 'minus';
    $lowName = array(
         /* zero is shown as "" since it is never used in combined forms */
         /* 0 .. 19 */
         "", "one", "two", "three", "four", "five",
         "six", "seven", "eight", "nine", "ten",
         "eleven", "twelve", "thirteen", "fourteen", "fifteen",
         "sixteen", "seventeen", "eighteen", "nineteen");
   
    $tys = array(
         /* 0, 10, 20, 30 ... 90 */
         "", "", "twenty", "thirty", "forty", "fifty",
         "sixty", "seventy", "eighty", "ninety");
   
    $groupName = array(
         /* We only need up to a quintillion, since a long is about 9 * 10 ^ 18 */
         /* American: unit, hundred, thousand, million, billion, trillion, quadrillion, quintillion */
         "", "hundred", "thousand", "million", "billion",
         "trillion", "quadrillion", "quintillion");
   
    $divisor = array(
         /* How many of this group is needed to form one of the succeeding group. */
         /* American: unit, hundred, thousand, million, billion, trillion, quadrillion, quintillion */
         100, 10, 1000, 1000, 1000, 1000, 1000, 1000) ;
   
    $num = str_replace(",","",$num);
    $num = number_format($num,2,'.','');
    $cents = substr($num,strlen($num)-2,strlen($num)-1);
    $num = (int)$num;
   
    $s = "";
   
    if ( $num == 0 ) $s = $ZERO;
    $negative = ($num < 0 );
    if ( $negative ) $num = -$num;
    // Work least significant digit to most, right to left.
    // until high order part is all 0s.
    for ( $i=0; $num>0; $i++ ) {
       $remdr = (int)($num % $divisor[$i]);
       $num = $num / $divisor[$i];
       // check for 1100 .. 1999, 2100..2999, ... 5200..5999
       // but not 1000..1099,  2000..2099, ...
       // Special case written as fifty-nine hundred.
       // e.g. thousands digit is 1..5 and hundreds digit is 1..9
       // Only when no further higher order.
       if ( $i == 1 /* doing hundreds */ && 1 <= $num && $num <= 5 ){
           if ( $remdr > 0 ){
               $remdr = ($num * 10);
               $num = 0;
           } // end if
       } // end if
       if ( $remdr == 0 ){
           continue;
       }
       $t = "";
       if ( $remdr < 20 ){
           $t = $lowName[$remdr];
       }
       else if ( $remdr < 100 ){
           $units = (int)$remdr % 10;
           $tens = (int)$remdr / 10;
           $t = $tys [$tens];
           if ( $units != 0 ){
               $t .= "-" . $lowName[$units];
           }
       }else {
           $t = num2words($remdr, 0);
       }
       $s = $t." ".$groupName[$i]." ".$s;
       $num = (int)$num;
    } // end for
    $s = trim($s);
    if ( $negative ){
       $s = $MINUS . " " . $s;
    }
   
    if ($c == 1) $s .= " and $cents/100";
   
    return $s;
} // end num2words
ck at vowel dot se
26-May-2006 10:11
Again, the function for returning file size in human readable format, but with a tweek for displaying one decimal when size is less than 10 units.

<?php
function human_readable( $size )
{
  
$count = 0;
  
$format = array("B","KB","MB","GB","TB","PB","EB","ZB","YB");
   while((
$size/1024)>1 && $count<8)
   {
      
$size=$size/1024;
      
$count++;
   }
   if(
$size < 10 ) $decimals = 1;
   else
$decimals = 0;
  
$return = number_format($size,$decimals,'.',' ')." ".$format[$count];
   return
$return;
}
?>

Thanks to "php dot net at alan-smith dot no-ip dot com" and "service at dual-creators dot de".
rudie at jouwmoeder dot nl
22-May-2006 09:30
number_format is pretty fast. It's much slower than calculation though:
<?php

$start
= microtime(true);

echo
"<pre>";
for (
$i=0;$i<100000;$i++)
{
    echo
number_format(microtime(true)-$start,25)."\n";
}
echo
"<hr />Parsetime: ".(microtime(true)-$start);

?>
This takes 1.03 seconds all the time. Without the -$start in the number_format cmd, it takes exactly as long (I ran it about 20 times).
The simple calculation inside number_format takes relatively no time. Defining the number before entering it into number_format also gives no change:
{
    $num = microtime(true)-$start;
    echo number_format($num,25)."\n";
}

Which concludes it takes about .01 sec to do a thousand number_format.
If you'd do the same routine without the number_format, it'd take .75 seconds -> number_format very fast!
gfinale at hotmail dot com
14-May-2006 05:58
Thanks to armstrong ~~at~~ rice ~~dot~~ edu for your number to words function.  There does seem to be one glitch I've discovered, at least on my system.  Hundreds work but hundreds of thousands and hundreds of millions do not.  e.g. 100,000 or 100,000,000 hangs.
chris_cember at cinmach dot com
15-Mar-2006 09:40
// the mktime shortcut is a little trickier than it would first appear, even with a mod...

function text_number($n) {
    $mod = $n % (($n > 20)?10:20);
    return $n . (($mod==0)?"th":date("S",mktime(0,0,0,1,$mod,2000)));
}  

// or for those who don't appreciate shorthand:

function text_number($n) {
    if ($n > 20) {
        $mod_factor = 10;
    } else {
        $mod_factor = 20;
    }  
    $mod = $n % $mod_factor;
    if ($mod == 0) {
        $txt = "th";
    } else {
        $txt = date("S",mktime(0,0,0,1,$mod,2000));
    }  
    return $n . $txt;
}

/*
one issue comes up in numbers that mod to zero (which mktime will associate with the 31st of December, 1999).  another issue is between 23rd and 13rd, err I mean 13th.  If you use 20 (or even 30) all the time, however, you'll end up with 33th (or 43th).
*/
Steve Neill
04-Mar-2006 12:04
actually, you'd want to MOD the number to get it within a useful range first (1 .. 30).

Enjoy.
Steve Neill
03-Mar-2006 10:15
why go to all that trouble?

date('S', mktime(0,0,0,1,$str,2000));

works just as well!
mikesabatino at gmail dot com
01-Mar-2006 03:34
<?php
# Function to represent a number like '2nd', '10th', '101st' etc
function text_number($n)
{
   
# Array holding the teen numbers. If the last 2 numbers of $n are in this array, then we'll add 'th' to the end of $n
   
$teen_array = array(11, 12, 13, 14, 15, 16, 17, 18, 19);
   
   
# Array holding all the single digit numbers. If the last number of $n, or if $n itself, is a key in this array, then we'll add that key's value to the end of $n
   
$single_array = array(1 => 'st', 2 => 'nd', 3 => 'rd', 4 => 'th', 5 => 'th', 6 => 'th', 7 => 'th', 8 => 'th', 9 => 'th', 0 => 'th');
   
   
# Store the last 2 digits of $n in order to check if it's a teen number.
   
$if_teen = substr($n, -2, 2);