PHP:区域设置感知数字格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/437371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP: Locale aware number format
提问by
I want to format the number 3253454 for my website visitors.
我想为我的网站访问者设置数字 3253454 的格式。
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
如果我使用内置的 number_format 函数,我会得到: 3,253,454 这对英国和美国来说很棒,但是大多数其他国家/地区使用 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?
理想情况下,我希望获取浏览器的语言环境并相应地格式化数字。这在PHP中甚至可能吗?
回答by kellan
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:
如果您要部署本地化网站,则需要确保设置了setlocale()。为了重复 yaauie 的上述帖子,我会在您的初始化代码中添加类似以下代码片段的内容:
$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:
然后我们修改上面的函数number_format_locale(),看起来像这样:
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.
当然,这是在理想情况下,根据您部署到的平台以及您安装的区域设置文件的版本,您可能必须围绕一些不规则进行编码。但是设置语言环境将有助于金钱、数字和日期。
回答by Pawe? Tomkiel
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.
我建议使用PHP NumberFormatter。这是一种使用ICU 库的 OOP 方法。
$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.
您可以在那里使用许多不同的样式格式,例如:DECIMAL、CURRENCY 或 PERCENT。在这里阅读更多。
It's best way of number formatting, because it depends on global Unicode Common Locale Data Repository.
这是数字格式化的最佳方式,因为它取决于全局Unicode Common Locale Data Repository。
回答by Karsten
string number_format (float $number, int $decimals, string $dec_point, string $thousands_sep)
see php doku for number_format
有关 number_format 的信息,请参阅php doku
Another helpful link may be Zend_Localefrom Zend Framework - it can detect your user's language and also help with number/currency formatting
另一个有用的链接可能是Zend Framework 中的Zend_Locale- 它可以检测您用户的语言,还可以帮助处理数字/货币格式
回答by Chris
PHP meanwhile offers a NumberFormatter Class which is perfect for this purpose:
PHP 同时提供了一个 NumberFormatter 类,它非常适合这个目的:
回答by Salty
From http://us3.php.net/manual/en/function.number-format.php#76448:
来自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. :)
这似乎正是你要找的。:)
回答by Andron
In my project I use Zend Framework. In this case I use such thing:
在我的项目中,我使用 Zend Framework。在这种情况下,我使用这样的东西:
$locale = new Zend_Locale('fr_FR');
$number = Zend_Locale_Format::toNumber(2.5, array('locale' => $locale));
// will return 2,5
print $number;
回答by yaauie
You could use the HTTP_ACCEPT_LANGUAGE server variable to guess their locale and expected number format.
您可以使用 HTTP_ACCEPT_LANGUAGE 服务器变量来猜测它们的语言环境和预期的数字格式。
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);
}

