Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php how to get the current decimal/thousand separator for a given locale

I've set the locale using the setlocale() function to let's say "en_US".

now I'm trying to format the currency without a thousand separator like this:

$currency = number_format($value, 2, '.', '');

that works, but sometimes I have other currencies and I want the number_format to use the correct decimal separator according to the use locale.

Is there a way to get the current decimal separator somehow, based on the locale that has been set?

like image 712
Jorre Avatar asked Feb 09 '11 11:02

Jorre


2 Answers

It is not the best, but it works:

$locale_info = localeconv();
echo 'decimal_point=' . $locale_info['decimal_point'] . "<br/>\n";

localeconv() returns an associative array with other fields besides the decimal point character.

like image 113
Rafael E. Chacón Avatar answered Sep 28 '22 04:09

Rafael E. Chacón


localeconv() should do it. From the manual:

Returns an associative array containing localized numeric and monetary formatting information.

Here is a list of language strings recognized by setlocale() on Windows.

like image 43
Sean Bright Avatar answered Sep 28 '22 03:09

Sean Bright