Question

I have this

$example = "1234567"
$subtotal =  number_format($example, 2, '.', '');

the return of $subtotal is "1234567.00" how to modify the definition of $subtotal, make it like this "1,234,567.00"

Was it helpful?

Solution

Below will output 1,234,567.00

$example = "1234567";
$subtotal =  number_format($example, 2, '.', ',');
echo $subtotal;

Syntax

string number_format ( float $number , int $decimals = 0 , string $dec_point = '.' , string $thousands_sep = ',' )

But I advise you to use money_format which will formats a number as a currency string

OTHER TIPS

You have many options, but money_format can do the trick for you.

// Example:

$amount = '100000';
setlocale(LC_MONETARY, 'en_IN');
$amount = money_format('%!i', $amount);
echo $amount;

// Output:

"1,00,000.00"

Please note that money_format() is only defined if the system has strfmon capabilities. For example, Windows does not, so it is undefined in Windows.

Final edit: Here's a pure PHP Implementation that will work on any system:

$amount = '10000034000';
$amount = moneyFormatIndia( $amount );
echo number_format($amount, 2, '.', '');

function moneyFormatIndia($num){
    $explrestunits = "" ;
    if(strlen($num)>3){
        $lastthree = substr($num, strlen($num)-3, strlen($num));
        $restunits = substr($num, 0, strlen($num)-3); // extracts the last three digits
        $restunits = (strlen($restunits)%2 == 1)?"0".$restunits:$restunits; // explodes the remaining digits in 2's formats, adds a zero in the beginning to maintain the 2's grouping.
        $expunit = str_split($restunits, 2);
        for($i=0; $i<sizeof($expunit); $i++){
            // creates each of the 2's group and adds a comma to the end
            if($i==0){
                $explrestunits .= (int)$expunit[$i].","; // if is first value , convert into integer
            }else{
                $explrestunits .= $expunit[$i].",";
            }
        }
        $thecash = $explrestunits.$lastthree;
    } else {
        $thecash = $num;
    }
    return $thecash; // writes the final format where $currency is the currency symbol.
}

Ref: http://php.net/manual/en/function.money-format.php

string money_format ( string $format , float $number )

ex:

// let's print the international format for the en_US locale
setlocale(LC_MONETARY, 'en_US');
echo money_format('%i', $number) . "\n";
// USD 1,234.56

Note: The function money_format() is only defined if the system has strfmon capabilities. For example, Windows does not, so money_format() is undefined in Windows.

Note: The LC_MONETARY category of the locale settings, affects the behavior of this function. Use setlocale() to set to the appropriate default locale before using this function.

Use number_format : http://www.php.net/manual/en/function.number-format.php

string number_format ( float $number , int $decimals = 0 , string $dec_point = '.' , string $thousands_sep = ',' )

$number        = 123457;
$format_number = number_format($number, 2, '.', ',');
// 1,234.57

Looks like money_format is deprecated now, so even if it's available to you, you should probably find another formatting solution.

https://www.php.net/manual/en/function.money-format.php

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top