'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?

php


Solution 1:[1]

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

Solution 2:[2]

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

Solution 3:[3]

 $number = 1234.56;

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

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

//1.234,56

Solution 4:[4]

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.

Solution 5:[5]

This is a bangladeshi format

First create a function

    function numberFormat($number, $decimals=0)
    {

        // $number = 555;
        // $decimals=0;
        // $number = 555.000;
        // $number = 555.123456;

        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';
            }
        }
        // return $decimalNumbers;



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

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

        for ($i = 0; $i < $stringlength; $i++)
        {
            if ($i%2==0 && $i!=$stringlength-1 && $i>1)
            {
                $n = $n.$number[$i].',';
            }
            else
            {
                $n = $n.$number[$i];
            }
        }

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

        ($decimals!=0)? $number=$number.'.'.$decimalNumbers : $number ;

        return $number;
    }

Call the function

* numberFormat(5000000, 2)  // 50,00,000.00 
* numberFormat(5000000)  // 50,00,000 

Solution 6:[6]

Give it a try:

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 oldboy
Solution 2 Saty
Solution 3 Ganesh Rengarajan
Solution 4 Geoff Kendall
Solution 5 Md. Saifur Rahman
Solution 6 sin906