Print Currency Number Format in PHP(在 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.
这篇关于在 PHP 中打印货币数字格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 PHP 中打印货币数字格式
基础教程推荐
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 如何替换eregi() 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
