Sam
Sam

Reputation: 3

Django math question

I have the following code in my template:

{{ object.rating.get_percent|floatformat|add:"-100" }}

Which outputs -50

The value of object.rating.get_percent is 50. I want to subtract it from the number 100. So I am expecting 50 in return. Why do I get -50?

Upvotes: 0

Views: 227

Answers (1)

bradley.ayers
bradley.ayers

Reputation: 38372

You get -50 because "-100" is the second operand to add. The first operand is the result of object.rating.get_percent|floatformat.

Essentially your expression is:

50 + -100

If you're really desperate to calculate "100 - x", you can use the {% widthratio %} tag:

{% widthratio -100|add:object.rating.get_percent -100 100 %}

However, you should really just get the backend developers to add a template filter for you.

Upvotes: 3

Related Questions