如何使用 Twig 的 Intl 扩展提高百分比格式的小数精度? [英] How to increase decimal precision on percentage format using Twig's Intl extension?

查看:43
本文介绍了如何使用 Twig 的 Intl 扩展提高百分比格式的小数精度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 4 位小数,小数位数为 4.(最大值为 0.9999,最小值为 0.0000)

我正在使用 Twig 及其国际扩展.当我想呈现百分比数字时,小数会四舍五入.

{% 设置十进制 = 0.0850 %}{{decimal|localizednumber('decimal','double','fr-fr') }}//将输出0,085"{{decimal|localizednumber('decimal','double','zh-cn') }}//将输出0.085"{{ decimal|localizednumber('decimal','double','ar-bh') }}//将输出٠٫٠٨٥"{{ decimal|localizednumber('percent','double','fr-fr') }}//将输出8%"{{ decimal|localizednumber('percent','double','zh-cn') }}//将输出8%"{{ decimal|localizednumber('percent','double','ar-bh') }}//将输出 "% ٨"

我希望看到法语的 8,5 %、中文的 8.5% 和阿拉伯语的 % ٨٫٥.>

我尝试添加 double 参数,但它不会改变精度.

当我使用 Symfony 时,我试图以数字格式声明 2 位小数:

#config/twig.yaml枝条:#...数字格式:小数点:2

似乎国际扩展覆盖了这些设置.

我知道我可以做类似的事情

{{ (decimal*100)|localizednumber('decimal','double')}}%

但是%符号可以在某些语言的结果之前,在法语中,%符号之前有一个不间断的空格.

你看到我的错误了吗?你有解决办法吗?

解决方案

无法直接使用您正在使用的 Twig Intl 扩展来完成.

它没有设置精度的参数,它只是使用您选择的格式样式实例化 NumberFormatter,并将其用于 format()你的价值.

此处检查代码.

<小时>

还有另一个扩展程序有更多选项:https://github.com/twigphp/intl-extra

通过这个,您可以指定 NumberFormatter 支持的所有属性,如图 此处.

你会像这样使用它:

{% 设置十进制 = 0.0850 %}{{小数|格式数字(风格=百分比",语言环境=fr",{min_fraction_digit:1})}}

<小时>

或者,您可以利用 NumberFormatter 编写自己的扩展.

NumberFormatter::PERCENT 默认使用的模式是 #,##0 %,不包括小数精度.您可以按照这些规则 确实如此.

或者简单地调用 setAttribute(NumberFormatter::MIN_FRACTION_DIGITS, 1) 来指定您感兴趣的最小十进制数.

例如:

$formatter->setAttribute(NumberFormatter::MIN_FRACTION_DIGITS, 1);

最终实现取决于您,如果您需要在 Twig 模板中使用它,您必须创建您自己的(官方 Symfony docs).

此处查看更多示例.

I have a decimal number with 4 digits and a scale of 4. (Max is 0.9999 and min is 0.0000)

I'm using Twig and its intl extension. When I want to render a percent number, decimals are rounded.

{% set decimal = 0.0850 %}
{{ decimal|localizednumber('decimal','double','fr-fr') }} //WILL OUTPUT "0,085"
{{ decimal|localizednumber('decimal','double','zh-cn') }} //WILL OUTPUT "0.085"
{{ decimal|localizednumber('decimal','double','ar-bh') }} //WILL OUTPUT "٠٫٠٨٥"
{{ decimal|localizednumber('percent','double','fr-fr') }} //WILL OUTPUT "8 %"
{{ decimal|localizednumber('percent','double','zh-cn') }} //WILL OUTPUT "8%"
{{ decimal|localizednumber('percent','double','ar-bh') }} //WILL OUTPUT "% ٨"

I would like to see 8,5 % in French, 8.5% in chinese and % ٨٫٥ in arabic.

I tried to add the double parameter, but it does not change the precision.

As I am using Symfony, I tried to declare 2 decimals in number format:

<!-- lang-yml -->
#config/twig.yaml
twig:
    #...
    number_format:
        decimals: 2

It seems that the Intl extension is overriding these settings.

I know that I can do something like

{{ (decimal*100)|localizednumber('decimal','double')}}% 

But the % symbol can be before the result in some languages, in French, there is a non-breaking space before the % symbol.

Do you see my error? Do you have a solution?

解决方案

Can't be done directly with Twig Intl extension you are using.

It doesn't have a parameter to set the precision, it simply instantiate the NumberFormatter with the formatting style of your choice, and will use that to format() your value.

Check the code here.


There is another extension which does have more options: https://github.com/twigphp/intl-extra

With this one you can specify all the attributes supported by NumberFormatter, as shown here.

You would use it like this:

{% set decimal = 0.0850 %}
{{ decimal|format_number(style='percent', locale='fr', {min_fraction_digit: 1}) }}


Alternatively, you could write your own extension by leveraging on NumberFormatter.

The pattern that NumberFormatter::PERCENT uses by default is #,##0 %, which does not include decimal precision. You can define your own pattern following these rules so it does.

Or simply call to setAttribute(NumberFormatter::MIN_FRACTION_DIGITS, 1) to specify the the minimum number of decimal numbers you are interested in.

For example:

$formatter->setAttribute(NumberFormatter::MIN_FRACTION_DIGITS, 1);

The final implementation is up to you, and if you'd need to use it within a Twig template you'd have to create your own (official Symfony docs).

See more examples here.

这篇关于如何使用 Twig 的 Intl 扩展提高百分比格式的小数精度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆