views:

907

answers:

5

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?

+2  A: 
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

Karsten
+1  A: 

http://us3.php.net/manual/en/function.number-format.php#76448

That seems to be exactly what you're looking for. :)

Salty
A: 

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 = ' ';
        case 'es':
        case 'es-mx':
        default:
            $decimal = ',';
            $thousands = ' ';
    }
    return $number_format($number,$decimals,$decimal,$thousands);
}
yaauie
Please note that I do not know (nor have researched) the expected formats of the above locales.
yaauie
A: 

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($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.

kellan
+1  A: 

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.

Orion Adrian