Domanda

I'm using PHP's number formatter on a local server and while it does work, it's printing out US dollar amounts like this: 8.500,00 $ when I actually want it to look like this: $8,500.00. Is there some parameter within the function that'll allow me to fix this problem or do I have to use something else?

È stato utile?

Soluzione

It looks like you have the wrong locale set in your NumberFormatter object. The format $8,500.00 is the USA's format. Make sure you've told NumberFormatter to use the correct locale.

Altri suggerimenti

Maybe something like this:

$number = 1234.52;
setlocale(LC_MONETARY, 'en_US');
$number = money_format('%#10.2n', $number);

Update oh, actually you said that you're using NumberFormatter. I just missed that. This should works fine as well:

$formatter = new NumberFormatter( 'en_US', NumberFormatter::CURRENCY );
$number = $formatter->format($number);

If you are using the number_format function make sure that you are using the decimal and comma in the right place in the function like the one below:

number_format ( $number , $decimals , '.' , ',' );

If you are using the NumberFormatter Class then I would recommend doing:

$number = 8500.00;
$fmt = new NumberFormatter( 'en_US', NumberFormatter::CURRENCY );
echo $fmt->format($number);
//Results in $8,500.00

There is an function called number_format

number_format ( $number , $decimals , $dec_separator , $thousands_separator)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top