doujiao1948 2014-05-26 18:56
浏览 35
已采纳

php number_format不使用locale

It's a first for me, using setLocale, I usually hate using it because I work on shared hosting, but since changing job recently, I have gained server control and thus the ability to install locales.

When I switch to fr_CA on my application, I call

money_format('%n', $value)

And it formats the currency correctly, but strangely, if I

number_format($value, 2);

I get a number formatted with the correct amount of decimals (2) but the decimal separator is not what was expected. I'm getting "0.32" instead of "0,32" like the locale says.

I even checked the locale on the server, and it clearly shows that the decimal separator is a comma:

LC_NUMERIC
decimal_point             "<U002C>"
thousands_sep             "<U0020>"
grouping                  3;3
END LC_NUMERIC

I checked online and U002C is indead comma... not period...

What's your take on this?

PS, here's what i checked:

  1. setLocale does return fr_CA
  2. setLocale clearly formats correctly and my code is not overriding the symbols in the call to number_format

Thanks

  • 写回答

1条回答 默认 最新

  • douci1541 2014-05-26 21:54
    关注

    The alternative is to use localeconv() to retrieve that information and use it in number_format(), something like:

    $locale = localeconv(); 
    echo number_format(
        $number, 
        $locale['frac_digits'], 
        $locale['decimal_point'], 
        $locale['thousands_sep']
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么