Open In App

Format Numbers in Django Templates

Last Updated : 07 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Formatting numbers in Django templates is essential for creating a polished and user-friendly interface. Whether you use Django’s built-in filters for common formatting needs, create custom filters for specific requirements, or leverage third-party libraries for advanced options, Django provides the tools necessary to present numerical data effectively.

Built-in Number Formatting Filters

Django comes with a set of built-in template filters that can help format numbers in various ways. These filters can be applied directly to your templates to achieve common formatting needs.

1. floatformat Filter

The floatformat filter is used to format floating-point numbers by specifying the number of decimal places.

{{ value|floatformat:2 }}

Example:

If value is 1234.56789, the template code:

{{ value|floatformat:2 }}

will render:

1234.57

If you omit the parameter, floatformat defaults to zero decimal places:

{{ value|floatformat }}

will render:

1235

Custom Template Filters

For more specific formatting needs, you can create custom template filters. This allows you to tailor the formatting to your particular requirements.

Define the Filter

Create a Python function to handle the formatting logic. For example, if you want to format numbers as currency with a dollar sign and two decimal places:

Python
from django import template

register = template.Library()

@register.filter
def currency(value):
    try:
        return "${:,.2f}".format(value)
    except (ValueError, TypeError):
        return value

Use the Filter in Templates

Make sure to register the filter in your templatetags module. Load your custom filter in the template and use it:

HTML
{% load your_custom_filters %}

{{ value|currency }}

Example:

If value is 1234.5678, the template code:

{{ value|currency }}

will render:

$1,234.57

Formatting Numbers Using Third-Party Libraries

Sometimes, you might need more advanced formatting options than what Django provides out of the box. Third-party libraries like Babel offer additional formatting capabilities.

Install Babel

You can install Babel via pip:

pip install Babel

Configure Babel in Django

Integrate Babel with Django to use its formatting capabilities. Create a custom filter that uses Babel’s formatting functions:

Python
from babel.numbers import format_currency
from django import template

register = template.Library()

@register.filter
def local_currency(value, currency='USD'):
    try:
        return format_currency(value, currency, locale='en_US')
    except (ValueError, TypeError):
        return value

Use the Filter in Templates

Load and use your custom filter in the template:

HTML
{% load your_custom_filters %}

{{ value|local_currency }}

Example:

If value is 1234.5678, the template code:

{{ value|local_currency }}

will render:

$1,234.57

Next Article
Article Tags :
Practice Tags :

Similar Reads