How to convert a negative number to a positive number in a django template?
{% for balance in balances %} {{ balance.amount }} {% endfor %}
If balance.amount is a negative number, I want to convert it to a positive number.
I would suggest installing django-mathfilters .
Then you can simply use the abs filter as follows:
abs
{% for balance in balances %} {{ balance.amount|abs }} {% endfor %}
from this SO :
{% if qty > 0 %} Please, sell {{ qty }} products. {% elif qty < 0 %} Please, buy {{ qty|slice:"1:" }} products. {% endif %}
If you do not want / cannot install django-mathfilters
You can make your own filter pretty easily:
from django import template register = template.Library() @register.filter(name='abs') def abs_filter(value): return abs(value)
This works without adding django-mathfilters, but this is not a good practice.
{% if balance.amount < 0 %} {% widthratio balance.amount 1 -1 %} {% else %} {{ balance.amount }} {% endif %}
Widthratio is for creating histograms, but can be used for multiplication
Source: https://habr.com/ru/post/977969/More articles:CMake: Attempting to add a targeting link library that is not built in this directory - c ++Can I put a throw declaration in the signature of a typedef function? - c ++OpenID Connect: Resource Owner Credentials - oauth-2.0Spring AMQP RabbitMQ implementing priority queue - javaGetting return values from Task.WhenAll - c #MongoDB _id map using Play-Reactivemongo plugin? - scalaHow to create a .NET client for the wso2 secure token service - vb.netXcode 6.1 fast autocomplete and the meaning of the code is broken - iosXcode 6.1 Swift Extensions - SourceKit Service Fails - swiftSending a single message using APNS - push-notificationAll Articles