How to separate numbers with commas 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?

Ben

52.2k48 gold badges171 silver badges217 bronze badges

asked Jan 3, 2011 at 6:49

Ahmad FouadAhmad Fouad

3,80714 gold badges44 silver badges62 bronze badges

0

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

I'm assuming you want the english format.


my 2 cents

oldboy

5,2544 gold badges26 silver badges74 bronze badges

answered Jan 3, 2011 at 6:53

0

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

Saty

22.3k7 gold badges30 silver badges49 bronze badges

answered Aug 31, 2013 at 11:24

Kausha MehtaKausha Mehta

2,7801 gold badge19 silver badges31 bronze badges

0

 $number = 1234.56;

//Vietnam notation[comma for decimal point, dot for thousand separator]

 $number_format_vietnam = number_format[$number, 2, ',', '.'];

//1.234,56

answered Jul 30, 2013 at 6:21

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

answered Apr 18 at 5:24

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.

answered Jun 19, 2013 at 16:37

Give it a try:

function format_my_number[] {
 $result = number_format[14301,2,',','.'];
 return $result;
}

answered May 12 at 8:07

How do you print a number with commas as thousands separators in PHP?

The number_format[] function rounds numbers and adds commas as a thousands separator..
number_format[$n] rounds $n to the nearest whole number and adds commas in between thousands. ... .
number_format[$n,$p] rounds $n to $p decimal places, adding commas between thousands..

How should comma separated values be stored in PHP?

The comma separated list can be created by using implode[] function. The implode[] is a builtin function in PHP and is used to join the elements of an array. implode[] is an alias for PHP | join[] function and works exactly same as that of join[] function.

What is number format PHP?

The number_format[] function is an inbuilt function in PHP which is used to format a number with grouped thousands. It returns the formatted number on success otherwise it gives E_WARNING on failure. Syntax: string number_format [ $number, $decimals, $decimalpoint, $sep ]

Chủ Đề