在 PHP 中打印货币数字格式
我有一些价格值要显示在我的页面中.
I have some price values to display in my page.
我正在编写一个函数,它采用浮动价格并返回带有货币代码的格式化货币 val..
I am writing a function which takes the float price and returns the formatted currency val with currency code too..
例如,fnPrice(1001.01)
应该打印 $ 1,000.01
推荐答案
最简单的答案是number_format()
.
The easiest answer is number_format()
.
echo "$ ".number_format($value, 2);
如果您希望您的应用程序能够使用多种货币和支持区域设置的格式(例如,对于我们一些欧洲人来说是 1.000,00
),它会变得有点复杂.
If you want your application to be able to work with multiple currencies and locale-aware formatting (1.000,00
for some of us Europeans for example), it becomes a bit more complex.
有 money_format()
但它不适用于 Windows并且依赖于setlocale()
,在我看来这很垃圾,因为它需要在服务器端安装(任意命名的)语言环境包.
There is money_format()
but it doesn't work on Windows and relies on setlocale()
, which is rubbish in my opinion, because it requires the installation of (arbitrarily named) locale packages on server side.
如果您想认真国际化您的应用程序,请考虑使用完整的国际化库,例如 Zend Framework 的 Zend_Locale 和 Zend_Currency.
If you want to seriously internationalize your application, consider using a full-blown internationalization library like Zend Framework's Zend_Locale and Zend_Currency.
相关文章