我认为这是一个相当愚蠢的问题,我研究了格式化数字以转换为价格格式的方法。我正在使用 smarty 作为模板系统。是否可以通过 smarty 或某种可用于此目的的方法来格式化价格?
我需要将类型数字 1000000 转换为 $1.000.000
smarty没有输出格式,还有什么方法?
谢谢
为 smarty 3.x 使用插件
http://www.smarty.net/docs/en/api.register.plugin.tpl
代码示例:
$smarty = new Smarty;
$smarty->registerPlugin("modifier", "number_format", "number_format");
在 smarty tpl 文件中,您可以像这样编写 html 代码:
...
<td style="text-align:right;border: 1px solid black;border-collapse: collapse;">{$upah|number_format:2:",":"."}</td>
...
注意:“,”是十进制符号,“.”是千分位器,可以互换。
强烈推荐使用国际化扩展类
NumberFormatter
PHP函数手册:
https://www.php.net/manual/en/numberformatter.formatcurrency.php
PHP类手册:https://www.php.net/manual/en/class.numberformatter.php
PHP扩展手册:https://www.php.net/manual/en/book.intl.php
工作完美。
PHP代码示例:
$smarty = new Smarty();
$smarty->registerPlugin('modifier', 'format_currency', [
NumberFormatter::create('en_US', NumberFormatter::CURRENCY),
'formatCurrency'
]);
Smarty模板:
{9.1|format_currency:USD} {100.19|format_currency:EUR}
输出
$9.10 €100.19
您可以使用默认语言环境,而不是“en_US”
locale_get_default()