Can anyone explain how to set “,” commas to numbers? For example, I have 22222.22$ dollars and I want to display it like this 22,222.22$.
how do I do that?
Convert the number from 22222.22$ to 22,222.22$.
Thanks in advace.
Can anyone explain how to set “,” commas to numbers? For example, I have 22222.22$ dollars and I want to display it like this 22,222.22$.
how do I do that?
Convert the number from 22222.22$ to 22,222.22$.
Thanks in advace.
The solution is very simple, use the PHP built-in function number_format();
For your case the solution would be like this
<?php
$number = 22222.22;
echo number_format($number, 2, '.', ',');
?>
Let’s try to understand what the function number_format() does. This might help others if anyone else has similar problems.
<?php
$number = 22222.2222;
// add thousands comma separator but remote decimal
echo number_format($number);
// 22,222
// Keep 2 decimal point and thousands comma separator
echo number_format($number, 2);
// 22,222.22
// Keep 2 decimal point and without thousands separator
echo number_format($number, 2, '.', '');
// 22222.22
// French notation
echo number_format($number, 2, ',', ' ');
// 22 222,22
//Vietnam notation(comma for decimal point, dot for separator)
echo number_format($number, 2, ',', '.');
// 22.222,22
?>
I hope by now you understand how the comma separator works. The last 2 parameters are used to set decimal point and thousands comma seperator