akabaka akabaka - 7 months ago 53
PHP Question

formatting currency for google merchant center

to use approved google merchant, I need to send them the price in this format:

  • 2,00 EUR must been written as 2.00 ou 2.

  • 100 000,00 EUR must be written 100000 or 100000.00, and not 100,000.00, 100,000 or +100,000.

  • Negatives values, like -20,14 EUR, must be written -20.14.

what I've got in my database actually is written like 100.000,00€

so to replace it, I've use:

$total_price = substr(str_replace(",", ".", str_replace(".", "", $order->info['total'])),0, -6);

Which is quite bad in my opinion. any thoughts on this?


You can use NumberFormatter::parseCurrency to parse a string into a double and a currency using the current formatter.