2

I am trying to format my number in the following way

12,34,56,789.00

i tried

number_format($row{'money'},2,".",",")

I am getting

123,456,789.00 

This is not what i wanted. So, How to do this?? (12,34,56,789.00)

Benjamin Toueg
  • 10,511
  • 7
  • 48
  • 79
LynAs
  • 6,407
  • 14
  • 48
  • 83
  • 4
    The `number_format()` method formats a number within defined/standard groups (i.e. thousands, millions, etc). Your requested format, 2 numbers + comma + 2 numbers + comma, is not a standard format. You'll have to write your own function to do that grouping (possibly with `money_format()`). – newfurniturey Oct 11 '13 at 14:16
  • 1
    You can’t do that with number_format, because the functions purpose is explicitly _“format a number with grouped thousands”_. – CBroe Oct 11 '13 at 14:18

1 Answers1

4

You can do this just by using the PHP function called money_format

$amount = '123456789.00';
setlocale(LC_MONETARY, 'en_IN');
$amount = money_format('%!i', $amount);
echo $amount; // 12,34,56,789.00

The function money_format() is only defined if the system has strfmon capabilities. For example, Windows does not, so money_format() is undefined in Windows.

So you can use this php code:

setlocale(LC_ALL, ''); // Locale will be different on each system.
$amount = 123456789.00;
$locale = localeconv();
echo number_format($amount, 2, $locale['decimal_point'], $locale['thousands_sep']);
Sriraman
  • 7,658
  • 4
  • 40
  • 60
  • 1
    Is it working ? , if you keep $amount as 123456789 ( without .00 ) – Sriraman Oct 11 '13 at 14:34
  • 1
    Really sorry, Please refer the following questions.. So, you can get the idea.. http://stackoverflow.com/questions/6369887/how-we-can-use-money-format-function-in-php-on-windows-platform http://stackoverflow.com/questions/9974704/cross-platform-money-format-linux-and-windows – Sriraman Oct 11 '13 at 16:26