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:
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
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.
from the php manual http://php.net/manual/en/function.number-format.php
I'm assuming you want the english format.
<?php
$number = 1234.56;
// english notation (default)
$english_format_number = number_format($number);
// 1,235
// French notation
$nombre_format_francais = number_format($number, 2, ',', ' ');
// 1 234,56
$number = 1234.5678;
// english notation with a decimal point and without thousands seperator
$english_format_number = number_format($number, 2, '.', '');
// 1234.57
?>
my 2 cents
$number = 1234.56;
//Vietnam notation(comma for decimal point, dot for thousand separator)
$number_format_vietnam = number_format($number, 2, ',', '.');
//1.234,56