Search code examples
drupaltwigdrupal-8drupal-webform

number_format usage in Drupal 8 Webform


I would like to format the numbers being displayed in a "Computed Twig" element in Drupal 8 Webform. I've tried Twig's "number_format" filter, but it does not work. I have this:

{{ (data.auswahl_oder_eingabe) * 202 / 1000000 * 25|number_format(2, ',', '.') }}

I would like to have only two decimals, the dot as a thousands separator and the comma as a decimal point. But I always get something like this:

53.025

So three decimals and the dot as a decimal separator. The format I would like to have in this case would be: 53,03 No matter what I write in that "number_format" definition, the result doesn't change. Does this filter work at all in Drupal 8.7.7? Or is there a mistake I made somewhere in the markup?


Solution

  • I believe that your filter is only applying to the 25 not the entire calculated value. Try something like this:

    {% set calculated = data.auswahl_oder_eingabe * 202 / 1000000 * 25 %}
    {{ calculated|number_format(2, ',', '.') }}