Forum Moderators: coopster

Message Too Old, No Replies

Trying to pass a particular format to number format()

         

neophyte

10:09 am on Jan 15, 2010 (gmt 0)

10+ Year Member



Hello All -

I'm trying to pass a particular format to number_format() but for some reason it doesn't like what I'm trying to do:

$format = '2, \',\', \'.\'';
$number = '1234567.89';

echo number_format($number, $format) , '<br>';

++++

What appears is 1,234,567.89 rather than 1.234.567,89.

It's gotta be (I guess) the way that I'm escaping the decimal and thousands separator but I've tried it a number of different ways but still can't get it right.

Any ideas what I'm doing wrong?

Neophyte

TheMadScientist

6:02 pm on Jan 15, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



$number = '1234567.89';
echo number_format($number, 2 , "," , ".") , '<br>';

From the Manual: (Emphasis Mine)

string number_format ( float $number , int $decimals , string $dec_point , string $thousands_sep )

PHP Number Format [us3.php.net]

The 2 below being a string, not an integer is one of the issues:
$format = '2, \',\', \'.\'';

I think it's best to just set this and not store it as a variable, because it's IMO not likely to change, so I really can't see the point in setting the extra variables... If you really need/want to for some reason you'll probably have to do this:

$decimal_places=2;
$decimal_separator=',';
$thousands='.';

$number = '1234567.89';
echo number_format($number, $decimal_places , $decimal_separator , $thousands) , '<br>';

neophyte

4:26 am on Jan 16, 2010 (gmt 0)

10+ Year Member



TheMadScientist -

Cool - thanks very much for the clear explanation; now I know what to do!

Neophyte