Ahmad Fouad
Ahmad Fouad

Reputation: 4107

How can I add commas to numbers in PHP

I would like to know how can I add comma's to numbers. To make my question simple.

I would like to change this:

1210 views

To:

1,210 views

and :

14301

to

14,301

and so on for larger numbers. Is it possible with a php function?

Upvotes: 90

Views: 136750

Answers (6)

Md. Saifur Rahman
Md. Saifur Rahman

Reputation: 404

This is a bangladeshi format

First create a function

function numberFormat($number, $decimals=0)
{
    if (strpos($number,'.')!=null)
    {
        $decimalNumbers = substr($number, strpos($number,'.'));
        $decimalNumbers = substr($decimalNumbers, 1, $decimals);
    }
    else
    {
        $decimalNumbers = 0;
        for ($i = 2; $i <=$decimals ; $i++)
        {
            $decimalNumbers = $decimalNumbers.'0';
        }
    }


    $number = (int) $number;
    $number = strrev($number);  // reverse

    $n = '';
    $stringlength = strlen($number);

    for ($i = 0; $i < $stringlength; $i++)
    {
        // from digit 3, every 2 digit () add comma
        if($i==2 || ($i>2 && $i%2==0) ) $n = $n.$number[$i].','; 
        else $n = $n.$number[$i];
    }

    $number = $n;    
    $number = strrev($number); // reverse

    ($decimals!=0)? $number=$number.'.'.$decimalNumbers : $number ;
    if ($number[0] == ',') $number = substr_replace($number, '', 0, 1);
    if ($number[1] == ',' && $number[0] == '-') $number = substr_replace($number, '', 1, 1);      

    return $number;
}

Call the function

* numberFormat(5000000, 2)  // Output: 50,00,000.00
* numberFormat(5000000)  // Output: 50,00,000
* numberFormat(-15264.61, 2)  // Output: -15,264.61
* numberFormat(2106.02)  // Output: 2,106
* numberFormat(2.02)  // Output: 2
* numberFormat(21.02)  // Output: 21
* numberFormat(210.02)  // Output: 210
* numberFormat(2106.02)  // Output: 2,106
* numberFormat(21060.02)  // Output: 21,060
* numberFormat(210600.02)  // Output: 2,10,600
* numberFormat(2106000.02)  // Output: 21,06,000
* numberFormat(21060000.02)  // Output: 2,10,6,0,000
* numberFormat(210600000.02)  // Output: 21,06,00,000
* numberFormat(-210600000.0222, 2)  // Output: -21,06,00,000.02

Upvotes: 6

sin906
sin906

Reputation: 27

Give it a try:

function format_my_number() {
 $result = number_format(14301,2,',','.');
 return $result;
}

Upvotes: -1

andrewk
andrewk

Reputation: 3871

from the php manual http://php.net/manual/en/function.number-format.php

I'm assuming you want the english format.

<?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 with a decimal point and without thousands seperator
$english_format_number = number_format($number, 2, '.', '');
// 1234.57

?>

my 2 cents

Upvotes: 181

Kausha Mehta
Kausha Mehta

Reputation: 2928

The Following code is working for me, may be this is helpful to you.

$number = 1234.56;

echo number_format($number, 2, '.', ',');

//1,234.56

Upvotes: 41

kelv
kelv

Reputation: 49

 $number = 1234.56;

//Vietnam notation(comma for decimal point, dot for thousand separator)

 $number_format_vietnam = number_format($number, 2, ',', '.');

//1.234,56

Upvotes: 4

Geoff Kendall
Geoff Kendall

Reputation: 1415

Often, if a number is big enough to have commas in it, you might want to do without any numbers after a decimal point - but if the value you are showing could ever be small, you would want to show those decimal places. Apply number_format conditionally, and you can use it to both add your commas and clip off any irrelevant post-point decimals.

if($measurement1 > 999) {
    //Adds commas in thousands and drops anything after the decimal point
    $measurement1 = number_format($measurement1);
    }

Works well if you are showing a calculated value derived from a real world input.

Upvotes: 1

Related Questions