在PHP和数字格式中将setlocale设置为fr-FR [英] setlocale to fr-FR in PHP and number formatting

查看:81
本文介绍了在PHP和数字格式中将setlocale设置为fr-FR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

但是我在电话号码方面遇到了一些问题.页面的一部分使用了从Web服务获取的数据.我正在将返回值分配给这样的var:

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)

在英文版中,totalRating值可能是7.5,并返回诸如7.5的值(通过number_format运行它的原因是,如果它返回7,我希望它显示7.0)

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)

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

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

第250行是number_format

我认为对7,5的翻译搞砸了,但是不确定如何解决这个问题...

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

使用PHP 5.3以防有什么新东西对我有帮助

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

推荐答案

因此,经过大量研究,最终找到了答案.

So eventually found the answer after a lot of research.

PHP只能与标准的美国十进制分隔符一起使用,而不能与法语的分隔符配合使用.

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

 setlocale(LC_ALL, 'fr_FR'); 

已经完成了窍门

这篇关于在PHP和数字格式中将setlocale设置为fr-FR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆