Score:0

Computed multiplication

us flag

I have a webform where I am using the Twig Computed module to calculate an amount. It is simple and straight forward: I have a field that captures the amount and I'm using Twig to multiply it to find the fee; the fee amount is a fixed rate

The Computed Value/Markup is {{ data['license_amount']|number_format(2) * 0.019 }}. It works when data['license_amount'] is lower than 999; it shows a wrong total when data['license_amount'] is higher than 999.

If the amount is 500 the value is 9.5 as it should.

If the amount is 5000 the value is returned as 0.095

Ivaylo Tsandev avatar
us flag
I agree with the locale part in the comment. Thousands separator seem to play you tricks here. However, the only change I'd suggest in the case is to add the second(decimal_point) and third(thousand_sep) parameters as explained in twig.symfony.com/doc/2.x/filters/number_format.html and this should be perfectly fine.
Score:1
in flag

You should drop the number_format altogether (not sure what the requirement is), or place it at the end of the calculation, i.e.

{{ (data['license_amount'] * 0.019)|number_format(2) }}

number_format is a wrapper around PHP's number_format function

From PHP's documentation page:

// english notation (default)
$english_format_number = number_format($number);
// 1,235

// French notation
$nombre_format_francais = number_format($number, 2, ',', ' ');
// 1 234,56

It seems like you are running the calculation on something like 5'000 or 5,000 (depending on your locale), which would explain the results you are getting.

Good luck!

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.