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?
localized_number
function is calling via an attribute the max number of digitgetAttribute(NumberFormat::MAX_FRACTION_DIGITS)
. It seems that it is possible to set this global parameter. I will try to explore this last idea, but I think your answer is the best solution. – Pasquale