在PHP中使用NumberFormatter保留尾随零 [英] Preserve trailing zeroes using NumberFormatter in PHP

查看:88
本文介绍了在PHP中使用NumberFormatter保留尾随零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力将旧系统从PEAR的i18nv2类升级到PHP的内置INTL库.当我指定类似#,##0.##的模式并在类似12345.1000的数字​​上运行NumberFormatter::format()时,我得到12,345.1不应保留尾随零吗?考虑到在许多语言环境中,用逗号而不是小数字符表示小数,是否有一种添加尾随零的好方法?

I'm working on upgrading a legacy system from PEAR's i18nv2 class to PHP's built-in INTL library. When I specify a pattern like #,##0.## and run NumberFormatter::format() on a number like 12345.1000 I get 12,345.1 shouldn't trailing zeroes be preserved? Is there a good way to add trailing zeroes after the fact taking into consideration that in many locales, a decimal is represented by a comma instead of the decimal character?

解释为代码:

$n = new NumberFormatter('en_CA', NumberFormatter::PATTERN_DECIMAL, '#,##0.##');
echo $n->format(12345.10000);

推荐答案

http://icu-project.org/apiref/icu4c/classDecimalFormatSymbols.html

Symbol   Meaning
  0      a digit
  #      a digit, zero shows as absent

所以:

$n = new NumberFormatter('en_CA', NumberFormatter::PATTERN_DECIMAL, '#,##0.00');
echo $n->format(12345.10000);

收益:

12,345.10

这篇关于在PHP中使用NumberFormatter保留尾随零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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