无法直接使用您正在使用的 Twig Intl 扩展来完成。
它没有设置精度的参数,它只是实例化NumberFormatter
与您选择的格式样式,并将使用它format()
你的价值。
检查代码here https://stackoverflow.com/questions/51405508/symfony-4-1-twig-extension.
还有另一个扩展,它有更多选项:https://github.com/twigphp/intl-extra https://github.com/twigphp/intl-extra
通过这个你可以指定所有支持的属性NumberFormatter
, 如图所示here https://symfony.com/doc/current/templating/twig_extension.html.
你可以像这样使用它:
{% set decimal = 0.0850 %}
{{ decimal|format_number(style='percent', locale='fr', {min_fraction_digit: 1}) }}
或者,您可以通过利用来编写自己的扩展NumberFormatter
.
的模式NumberFormatter::PERCENT
默认使用的是#,##0 %
,不包括小数精度。您可以定义自己的模式如下这些规则 https://unicode-org.github.io/icu-docs/apidoc/released/icu4c/classDecimalFormat.html#details确实如此。
或者直接致电setAttribute(NumberFormatter::MIN_FRACTION_DIGITS, 1)
指定您感兴趣的最小小数位数。
例如:
$formatter->setAttribute(NumberFormatter::MIN_FRACTION_DIGITS, 1);
最终的实现取决于您,如果您需要在 Twig 模板中使用它,您必须创建你自己的 https://stackoverflow.com/questions/51405508/symfony-4-1-twig-extension(官方交响乐团docs https://symfony.com/doc/current/templating/twig_extension.html).
查看更多示例here https://3v4l.org/UltjH.