Preserve trailing zeroes using NumberFormatter in PHP

对着背影说爱祢 提交于 2020-01-11 07:30:41

问题


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?

Explained with code:

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

回答1:


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

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

So:

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

Yields:

12,345.10


来源:https://stackoverflow.com/questions/36895787/preserve-trailing-zeroes-using-numberformatter-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!