在 PHP 中打印货币数字格式 [英] Print Currency Number Format in PHP

查看:22
本文介绍了在 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_LocaleZend_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.

这篇关于在 PHP 中打印货币数字格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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