-2

How to convert amount to lakh and crore in django. for example if amount is 100 000 I want to show 1 Lakh. Is it possible to do this by using a custom template tag?

I'm a beginner so this is the code I wrote to get this done. But how to use this in template tag?

if properties.expected_price >= 100000:
    expected_price_in = expected_price/100000
elif properties.expected_price >= 1000000:
    expected_price_in = expected_price/1000000
else:
    expected_price_in = expected_price
James Z
  • 12,209
  • 10
  • 24
  • 44

1 Answers1

1

You can find good explanation of Django custom template filter here and here. I'll just give you a brief description. First of all you need to creat folder templatetags inside your django app and add __init__.py file. Create in new folder some .py file for example custom_filters.py with following content:

from django import template

register = template.Library()

@register.filter
def num_format(value):
    if value >= 1000000:
        return value/1000000
    elif value >= 100000:
        return value/100000
    else:
        return value

Now you can use it in template like this:

{% load custom_filters %}
{{ your_number|num_format }}

But I also suggest you to look at humanize utils. Propably it can solve your problem.

just add into settings INSTALLED_APPS 'django.contrib.humanize' and try this in template:

{% load humanize %}
{{ your_number|intword }}
neverwalkaloner
  • 46,181
  • 7
  • 92
  • 100