Php add commas to numbers thousands

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.3k48 gold badges171 silver badges217 bronze badges

asked Jan 3, 2011 at 6:49

Ahmad FouadAhmad Fouad

3,81714 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,2744 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 write 1000000 with commas?

A comma is placed every third digit to the left of the decimal point and so is used in numbers with four or more digits. Continue to place a comma after every third digit. For example: $1,000,000 [one million dollars]

How can I set 2 decimal places in PHP?

Use number_format[] Function to Show a Number to Two Decimal Places in PHP..
Use round[] Function to Show a Number to Two Decimal Places in PHP..
Use sprintf[] Function to Show a Number to Two Decimal Places in PHP..
Related Article - PHP Number..

How do you put a comma in a foreach loop?

If your intention is to show comma after each element except the last one, you can easily use $loop->last[] inside your foreach.

Chủ Đề