Search code examples
phpnumber-formatting

Add commas as thousands separator and floating point dot in php


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"


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