Organizational Research By

Surprising Reserch Topic

php locale aware number format using -'php,internationalization'


php locale aware number format  using -'php,internationalization'

I want to format the number 3253454 for my website visitors.

If I use the inbuilt number_format function, I get: 3,253,454 which is great for UK and USA, however most other countries use 3.253.454

I have many international visitors.

Can anyone give me a pointer to the best practice here?

Ideally I wish to get the browser's locale and format the number accordingly. Is this even possible in PHP?
    

asked Sep 8, 2015 by rajesh
0 votes
8 views



Related Hot Questions

7 Answers

0 votes
If you're deploying a localized website, you're going to want to make sure you setlocale(). To riff off of yaauie's above post I'd add something like the following code snippet in your initialization code: $locale = ( isset($_COOKIE['locale']) ) ? $_COOKIE['locale'] : $_SERVER['HTTP_ACCEPT_LANGUAGE']; setlocale(LC_ALL, $locale); Then we modify the above function number_format_locale(), to look like so: function number_format_locale($number,$decimals=2) { $locale = localeconv(); return number_format($number,$decimals, $locale['decimal_point'], $locale['thousands_sep']); } Of course that's in an ideal world, depending on the platform you deploy to, and what version of the locale files you have installed, you might have to code around some irregularities. But setting locale is going to help with money, numbers, and dates.
answered Sep 8, 2015 by rajesh
0 votes
string number_format (float $number, int $decimals, string $dec_point, string $thousands_sep) see php doku for number_format Another helpful link may be Zend_Locale from Zend Framework - it can detect your user's language and also help with number/currency formatting
answered Sep 8, 2015 by rajesh
0 votes
From http://us3.php.net/manual/en/function.number-format.php#76448: <?php function strtonumber( $str, $dec_point=null, $thousands_sep=null ) { if( is_null($dec_point) || is_null($thousands_sep) ) { $locale = localeconv(); if( is_null($dec_point) ) { $dec_point = $locale['decimal_point']; } if( is_null($thousands_sep) ) { $thousands_sep = $locale['thousands_sep']; } } $number = (float) str_replace($dec_point, '.', str_replace($thousands_sep, '', $str)); if( $number == (int) $number ) { return (int) $number; } else { return $number; } } ?> That seems to be exactly what you're looking for. :)
answered Sep 8, 2015 by rajesh
0 votes
There's no easy answer to this question, because globalization is really freaking hard. There's a ton of variations on the style, including the thousands separator and where the separator goes (hint: it's not always thousands). Also negatives are handled very differently from country to country, sometimes placing them in parentheses and sometimes placing the negative after the number. So all that said, my advice. Forget about the globalization. If you're not actually going to support multiple locales fully just stick to the one locale you know well. People are generally accustomed (especially international folks) to seeing numbers formated differently than they themselves do it. Now if you're still determined to do it, I suggest leveraging one of the better globalization libraries out there. This is one of those places where you probably want to let the experts handle it for you.
answered Sep 8, 2015 by rajesh
0 votes
Maybe try approach independent from setting global locale for all scripts? Get user's locale: $locale = ( isset($_COOKIE['locale']) ) ? $_COOKIE['locale'] : $_SERVER['HTTP_ACCEPT_LANGUAGE']; Format the number: I recommend using PHP NumberFormatter. It's an OOP approach which uses ICU library. $formatStyle=NumberFormatter::DECIMAL; $formatter= new NumberFormatter($locale, $formatStyle); echo $formatter->format(3253454);//proper output depending on locale You can use many different style formats there, for example: DECIMAL, CURRENCY or PERCENT. Read more here. It's best way of number formatting, because it depends on global Unicode Common Locale Data Repository.
answered Sep 8, 2015 by rajesh
0 votes
In my project I use Zend Framework. In this case I use such thing: $locale = new Zend_Locale('fr_FR'); $number = Zend_Locale_Format::toNumber(2.5, array('locale' => $locale)); // will return 2,5 print $number;
answered Sep 8, 2015 by rajesh
0 votes
You could use the HTTP_ACCEPT_LANGUAGE server variable to guess their locale and expected number format. If I were to implement this, I'd allow a user to set a preference to override the guessed, and my function would look like this: function number_format_locale($number,$decimals=2) { $locale = ( isset($_COOKIE['locale']) ? $_COOKIE['locale'] : $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) switch($locale) { case 'en-us': case 'en-ca': $decimal = '.'; $thousands = ','; break; case 'fr': case 'ca': case 'de': case 'en-gb': $decimal = ','; $thousands = ' '; break; case 'es': case 'es-mx': default: $decimal = ','; $thousands = ' '; } return $number_format($number,$decimals,$decimal,$thousands); }
answered Sep 8, 2015 by rajesh

...