Add commas as thousands separator and floating point dot in php

Andrew Liu picture Andrew Liu · Jul 22, 2013 · Viewed 40.6k times · Source

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"

Answer

Techie picture Techie · Jul 22, 2013

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