开发者

Django math question

开发者 https://www.devze.com 2023-03-12 19:56 出处:网络
I have the following code in my template: {{ object.rating.ge开发者_如何学编程t_percent|floatformat|add:\"-100\" }}

I have the following code in my template:

{{ object.rating.ge开发者_如何学编程t_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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消