我认为这是一个相当愚蠢的问题,我研究了如何格式化数字以转换为价格格式。我正在使用smarty作为模板系统。有没有可能通过巧妙的方式或某种可以用于此目的的方法来格式化价格?
我需要将型号1000000转换为1.000.000美元
如果smarty没有输出格式,还有什么其他方法?
感谢
使用PHP的货币格式函数
在smarty中,这将是{$myNumber|money_format:'%i'}
使用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>
...
注意:"是十进制simbol和"是千个sparator,您可以交换它。
强烈建议使用国际化扩展类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'
]);
智能模板:
{9.1|format_currency:USD} {100.19|format_currency:EUR}
输出
$9.10 €100.19
您可以使用默认的区域设置locale_get_default()
,而不是"en_US"
PHP手册:https://www.php.net/manual/en/locale.getdefault.php