Smarty - 从逗号到逗号的字符串到货币

问题描述 投票:0回答:1

我有以下字符串:

$betrag = 4,9

想要将其转换为货币:

4,90 €

是否有更好的方法:

{$betrag|replace:",":"."|string_format:"%.2f"|replace:".":","} €
type-conversion smarty
1个回答
1
投票

好的解决方案是使用smarty修饰符:

function smarty_modifier_num2front($string, $precision = 2)
{
   // here you can use your own filter by call some class (or direct php code)
   return \lib\Numeric::front($string, $precision);
}

以下函数应位于smarty插件目录中,在本例中名为'modifier.num2front.php'

在模板中,你使用这样:

{$betrag|num2front}
© www.soinside.com 2019 - 2024. All rights reserved.