开发者社区> 问答> 正文

PHP中的NumberFormatter与JS中的NumberFormat

尝试格式化货币时,似乎得到了非常不一致的结果。在PHP中,我正在使用https://www.php.net/manual/zh/class.numberformatter.php。我有以下演示代码:

$number = 5125.99; echo getInternationallyFormattedCurrency($number, 'tl-PH', 'PHP'); echo '
'; echo getInternationallyFormattedCurrency($number, 'fil-PH', 'PHP'); echo '
'; echo getInternationallyFormattedCurrency($number, 'en-US', 'PHP'); echo '
'; echo '
'; echo getInternationallyFormattedCurrency($number, 'tl_PH', 'USD'); echo '
'; echo getInternationallyFormattedCurrency($number, 'fil_PH', 'USD'); echo '
'; echo getInternationallyFormattedCurrency($number, 'en_US', 'USD'); echo '
'; 当我在本地主机上运行此版本(版本为PHP 7.3.7,但已更新为与服务器的PHP版本7.3.12匹配)时,我得到了:

₱5,125.99 ₱5,125.99 PHP 5,125.99

$5,125.99 $5,125.99 $5,125.99 但是,当我在服务器(PHP版本7.3.12)上运行它时,会得到以下信息:

₱ 5125.99 ₱ 5125.99 ₱5,125.99

$ 5125.99 $ 5125.99 $5,125.99 为什么会有所不同?哪个实际上是正确的?

我也需要JS完全相同的功能。因此,在https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat上,我输入了以下等效代码:

var number = 5125.99;

console.log(new Intl.NumberFormat('tl-PH', { style: 'currency', currency: 'PHP' }).format(number)); console.log(new Intl.NumberFormat('fil-PH', { style: 'currency', currency: 'PHP' }).format(number)); console.log(new Intl.NumberFormat('en-US', { style: 'currency', currency: 'PHP' }).format(number)); console.log(new Intl.NumberFormat('tl-PH', { style: 'currency', currency: 'USD' }).format(number)); console.log(new Intl.NumberFormat('fil-PH', { style: 'currency', currency: 'USD' }).format(number)); console.log(new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format(number)); 我得到这个:

"PHP 5,125.99" "₱5,125.99" "PHP 5,125.99" "$5,125.99" "$5,125.99" "$5,125.99" 所以-这是另一个结果。我需要一种格式化PHP和JS之间的货币一致性的方法。我该怎么做呢?

更新1:

我的本地计算机的ICU版本为64.2,而服务器的ICU版本为4.2.1。我将查看托管服务提供商是否可以将ICU版本更新为最新版本。这可能可以解释本地计算机输出与服务器输出之间的差异。

仍然不确定为什么JS表现不同。

展开
收起
社区秘书 2019-12-09 16:50:11 606 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
阿里云栖开发者沙龙PHP技术专场-直面PHP微服务架构挑战-高驰涛 立即下载
PHP安全开发:从白帽角度做安全 立即下载
PHP 2017.北京 全球开发者大会——高可用的PHP 立即下载