PHP: Locale aware number format

女生的网名这么多〃 提交于 2019-12-28 04:01:14

问题


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?


回答1:


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.




回答2:


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.




回答3:


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




回答4:


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. :)




回答5:


PHP meanwhile offers a NumberFormatter Class which is perfect for this purpose:

http://php.net/manual/de/class.numberformatter.php




回答6:


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;



回答7:


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);
}


来源:https://stackoverflow.com/questions/437371/php-locale-aware-number-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!