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 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化