Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert negative number to positive number in django template?

Tags:

python

django

How to convert negative number to positive number in django template?

{% for balance in balances %}
    {{ balance.amount }}
{% endfor %}

If balance.amount is negative number, I want to convert it to positive number.

like image 860
kangfend Avatar asked Nov 10 '14 08:11

kangfend


People also ask

How do you convert a negative number to a positive number?

Declare an integer variable say ‘ num ‘ and take the value as user input. Check if the number is less than 0 then it is negative number then convert it to positive by using Math.abs () and print the positive number. Else the number is already a positive number then print the number.

How to change negative numbers to positive numbers in Google Sheets?

In this post, you will see several easy methods with which you can change negative numbers to positive numbers in Google Sheets. Using Find and Replace to change the number sign. Using multiplication to change the number sign. Using the ABS function to change the number sign. Using the UMINUS function to change the number sign.

What is positiveintegerfield in Django?

It supports values from 0 to 2147483647 are safe in all databases supported by Django.It uses MinValueValidator and MaxValueValidator to validate the input based on the values that the default database supports. Illustration of PositiveIntegerField using an Example. Consider a project named geeksforgeeks having an app named geeks.

How do I know if I'm using Django or not?

If value is "I'm using Django", the output will be "I\'m using Django". Capitalizes the first character of the value. If the first character is not a letter, this filter has no effect. If value is "django", the output will be "Django".


Video Answer


2 Answers

I would like to suggest installing django-mathfilters.

Then you can simply use the abs filter like this:

{% for balance in balances %}
    {{ balance.amount|abs }}
{% endfor %}
like image 131
Krystian Cybulski Avatar answered Sep 22 '22 19:09

Krystian Cybulski


If you don't want/can't install django-mathfilters

You can make a custom filter quite easily:

from django import template
register = template.Library()


@register.filter(name='abs')
def abs_filter(value):
    return abs(value)
like image 45
Marcs Avatar answered Sep 22 '22 19:09

Marcs