Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add commas as thousands separator and floating point dot in php

Tags:

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"

like image 365
Andrew Liu Avatar asked Jul 22 '13 05:07

Andrew Liu


People also ask

How do you format a thousands separator?

The character used as the thousands separatorIn the United States, this character is a comma (,). In Germany, it is a period (.). Thus one thousand and twenty-five is displayed as 1,025 in the United States and 1.025 in Germany. In Sweden, the thousands separator is a space.


1 Answers

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

like image 149
Techie Avatar answered Oct 07 '22 03:10

Techie