I have a a variable {{ product.amount }} and need to display the amount left of a multiplication of 6.
So for example {{ product.amount }} is 20. so it has to output 4. (for 24)
How can i do this?
You can directly use modulo in twig template, as documentation said :
{{ 6 - product.amount % 6 }}
You can use modulo to calculate the rest of the division by 6 and then calculate the difference
select 6-(20 % 6) as Solved
Related
This question already has answers here:
How to display a float with two decimal places?
(13 answers)
Closed 10 months ago.
How to print the calculation of float with only 3 more decimal
If I calculated something and the answer of that calculation is like, 3.33333333...
I just want to print it like 3.333 and that's it
With the round function:
round(3.33333333, 3)
You can use format specifiar
num = 3.3333333
print("%.3f"%num)
Output:3.333
a=1.233455543323
print('{0:.3f}'.format(a))
output=1.233
you can use round function
syntax:
round(number, digits)
number: Required. The number to be rounded
digits: Optional. The number of decimals to use when rounding the number. Default is 0
num = 3.33333333
print(round(num, 3))
I have a computed twig field in my webform that will calculate the total amount of money. Once it calculates the balance, I added an if statement that will change the title of the element (computed twig type) conditionally if the number calculated is smaller or equal to 0 from Total Reimbursement to Total Due Accounting then prints the absolute value of the number to avoid printing out a negative number. here is the if statement part in Computed value/markup I am having issue with (I will just attach the if statement part since I made sure the number is calculated correctly)
{% set reimbursement = (total_cost - total_amount)|round(2, 'ceil') %} {# calculating the total amount #}
{% if reimbursement <= 0 %}
{% [webform:element:total_reimbursement_01_total_reimbursement_:title] == 'Total Due Accounting' %} {# Here is where I am getting error at. I am trying to change the name of the title if the total amount is 0 or a negative number #}
{% set reimbursement = (total_amount - total_cost)|round(2, 'ceil') %}
{{'$ ' ~ reimbursement|number_format(2,'.',',') }}
{% else %}
{{'$ ' ~ reimbursement|number_format(2,'.',',') }}
{% endif %}
When I tried to save it in UI,I get an error that says Computed value/markup is not valid.
A block must start with a tag name in
"__string_template__7ee19d3ea967979a05c98764486ed96b961799aed3702bb3b9b515fd794f4099"
at line 23.
line 23 is the line where I tried to use the element's title. I believe this is how I pull the element's title, but seems like it's not.
Any help?
I want to add two three digit numbers, digit by digit, and need to get value of carry after addition without using IF/ELSE and While/For. For example, if I am adding 9 and 9, I need to get the carry of these numbers but without using IF/ELSE and WHILE/FOR. Ideas?
If I've understood your question properly, I think you can achieve this with integer division.
For Example, the carry for 9 + 9 would be: (9 + 9) // 10. This would return 1.
For three digit numbers you can do: (678 + 854) // 1000.
This question already has answers here:
How to get a float result by dividing two integer values using T-SQL?
(10 answers)
Closed 7 years ago.
I am trying to calculate some performance metrics as [RATE] in SQL but no matter what I do I am only getting integer values in return.
In my dataset I have a Numerator and a Denominator both stored as integers that are broken down into groups and subsets. The rate is calculated slightly differently depending on the group and subset. For group 1 the calculation is simply N/D. For group 2 the calculation is (N*1000)/D with the exception of 1 subset which is calculated (N*10000)/D.
I wrote the query as:
SELECT [Group]
,[SubSet]
,[Numerator] N
,[Denominator] D
,CASE WHEN D=0 Then NULL
WHEN [Group]='G1' THEN [N]/[D]
WHEN [SubSet]='S2' THEN ([N]*10000)/[D]
WHEN [SubSet] NOT LIKE 'S2%' AND [G] NOT LIKE 'G1%' THEN ([N]*1000)/[D] as [RATE]
No matter what I do the outcome variables are integers. I tried formatting RATE as varchar, decimal, and float with no success. I tried changing N and D's format to varchar, decimal, and float as well. I tried changing the equations from (N*1000)/D to (N/D)*1000 but still no effect.
What am I missing/doing wrong?
The problem you are having is because SQL is doing integer division, which will only return whole numbers. To get a decimal return value you must have at least one value as a decimal.
Try this:
(CAST([N] as decimal(12,6))/[D]) * 1000
Adjust decimal(12,6) based on the precision you are expecting. 12,6 will return a decimal with 6 digits after the decimal point. If you wanted only 2 decimal places use 16,2.
If you then want to round the calculated value you will need to make use of the ROUND function in SQL.
Round to the second decimal place:
ROUND((CAST([N] as decimal(12,6))/[D]) * 1000, 2)
You need to use CAST:
CAST ((N*1000) AS FLOAT) / D
Hope this helps.
SELECT (n * 1000.0) will do it.
The Twig documentation for number rounding talks about rounding decimals, but I have a case where I want to round a number like 19,995 to 20,000. Is there a tricky way to round up to nearest thousand?
The round filter accepts negative precision. (Just like in PHP)
{{ 19995|round(-3) }}
The simplest way is to divide your original number, round, then multiply again by 1000:
{% set amount = (19995 / 1000)|round %}
{{ amount * 1000 }}
Rounding to the nearest 10k or 100k would be as simple as changing 1000 to 10000 or 100000 respectively.
UPDATE: Or you can just use Matt Rose's suggestion below.
{{ 19995|round(-3) }}