在 PHP 中将语言环境设置为 fr-FR 和数字格式

发布于 2024-09-03 02:29:55 字数 863 浏览 1 评论 0原文

我正在尝试创建我的网站之一的法语版本。我已在页面顶部设置 setlocale(LC_ALL, 'fr_FR'); ,并使用 strftime 我以法国风格正确显示日期。

但是我在使用数字时遇到了一些问题。该页面的一部分使用我从 Web 服务获取的数据。我将返回值分配给这样的 var:

$overallRating = $returnArray['Overall'];

然后我稍后使用以下代码将其格式化为小数点后 1 位

number_format($overallRating,1)

在英语版本中,overallRating 值可能是 7.5 并返回一个值,例如7.5 (通过 number_format 运行它的原因是如果它返回 7 我希望它显示 7.0

但是在法语版本中如果我打印出原始的 $overallRating 值,我得到 7,5 ,看起来它已经将其翻译成法语。这很好,但如果我通过 number_format 运行它,我会收到错误:

Notice: A non well formed numeric value encountered in /sites/index.php  on line 250

第 250 行是 number_format 行,

我认为翻译为 7,5 搞砸了,但不知道如何解决这个问题...

使用 PHP 5.3,以防有任何新内容可以帮助我

I am trying to create a French version of one of my sites. I have set setlocale(LC_ALL, 'fr_FR'); at the top of my page, and using strftime I am displaying dates correctly in the French style.

However I am having some problems with a number. Part of the page uses data I am getting from a Web Service. I am assigning a returned value to a var like this:

$overallRating = $returnArray['Overall'];

I am then using the following code later to format it to 1 decimal place

number_format($overallRating,1)

In the English version the overallRating value might be 7.5 and returns a value such as 7.5 (the reason for running it through the number_format is if it returns 7 I want it to display 7.0)

However in the French version if I print out the raw $overallRating value I get 7,5 which looks like it has translated it into french. This would be fine but if I run it through number_format I get the error:

Notice: A non well formed numeric value encountered in /sites/index.php  on line 250

Line 250 is the number_format line

I presume the translation to 7,5 is messing it up, but not sure how to solve this...

Using PHP 5.3 in case there is anything new that helps me

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青芜 2024-09-10 02:29:55

于是经过大量研究,最终找到了答案。

PHP 仅适用于标准美国小数分隔符,无法处理法语 , 分隔符。

答案虽然并不完美,但是将数值重置为使用美国,同时允许将日期等设置为法语格式。放置这一行:

 setlocale(LC_NUMERIC, 'C');

Under

 setlocale(LC_ALL, 'fr_FR'); 

就完成了任务

So eventually found the answer after a lot of research.

PHP only really works with standard US decimal seperators and can't cope with the french , seperator.

The answer, although not perfect is to reset numeric values to use US while allowing dates etc to be formatted in French. Placing this line:

 setlocale(LC_NUMERIC, 'C');

Under

 setlocale(LC_ALL, 'fr_FR'); 

has done the trick

苏璃陌 2024-09-10 02:29:55

这就是你要问的吗?

<?php
    $overallRating = number_format($number, 1, '.', '');
?>

稍后编辑:

<?php
setlocale(LC_ALL, 'fr_FR');
$number = '7';
echo $overallRating = number_format($number, 1, '.', '');
?>

is this what you are asking ?

<?php
    $overallRating = number_format($number, 1, '.', '');
?>

Later Edit:

<?php
setlocale(LC_ALL, 'fr_FR');
$number = '7';
echo $overallRating = number_format($number, 1, '.', '');
?>
浮世清欢 2024-09-10 02:29:55

我想采用像“54,33”这样的法语编辑字符串并将其作为 en_US 数字保存回数据库,因为正如已经指出的那样,PHP 实际上只喜欢 en_US 格式的数字。我没有看到任何内置的 PHP 转换函数,因此构建了这个函数来完成这项工作。删除千位分隔符并用“.”替换小数分隔符。它设计用于任何设置。

//Currency Conversion Back to US for DB Storage or PHP number formatting
function number_convert_us($num) {
    $locale_settings = localeconv();
    $num = str_replace($locale_settings['mon_thousands_sep'], "", trim($num));
    $num = str_replace($locale_settings['mon_decimal_point'], ".", $num);
    return number_format((double)$num, 2, '.', '');
}

我将为 JavaScript 构建一个类似的东西。找到这个库用于显示: http://software.dzhuvinov.com/jsworld-currency-formatting .html

I wanted to take a French-edited string like "54,33" and save it back to the db as en_US number because, as has been pointed out, PHP really only likes numbers in the en_US format. I didn't see any built-in PHP conversion functions so built this to do the job. Removes the thousands separator and replaces the decimal separator with "." and it's designed to work with any setup.

//Currency Conversion Back to US for DB Storage or PHP number formatting
function number_convert_us($num) {
    $locale_settings = localeconv();
    $num = str_replace($locale_settings['mon_thousands_sep'], "", trim($num));
    $num = str_replace($locale_settings['mon_decimal_point'], ".", $num);
    return number_format((double)$num, 2, '.', '');
}

I'll build a similar thing for JavaScript. Found this library for display: http://software.dzhuvinov.com/jsworld-currency-formatting.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文