Format Numbers in Django Templates
Last Updated :
07 Aug, 2024
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
Similar Reads
Django Template Filters
Django Template Engine provides filters which are used to transform the values of variables and tag arguments. We have already discussed major Django Template Tags. Tags can't modify value of a variable whereas filters can be used for incrementing value of a variable or modifying it to one's own nee
6 min read
for loop - Django Template Tags
A Django template is a text document or a Python string marked-up using the Django template language. Django being a powerful Batteries included framework provides convenience to rendering data in a template. Django templates not only allow passing data from view to template, but also provides some
3 min read
lorem - Django Template Tags
A Django template is a text document or a Python string marked-up using the Django template language. Django being a powerful Batteries included framework provides convenience to rendering data in a template. Django templates not only allow passing data from view to template, but also provides some
2 min read
include - Django Template Tags
A Django template is a text document or a Python string marked-up using the Django template language. Django being a powerful Batteries included framework provides convenience to rendering data in a template. Django templates not only allow passing data from view to template, but also provides some
2 min read
firstof - Django Template Tags
A Django template is a text document or a Python string marked-up using the Django template language. Django being a powerful Batteries included framework provides convenience to rendering data in a template. Django templates not only allow passing data from view to template, but also provides some
3 min read
if - Django Template Tags
A Django template is a text document or a Python string marked-up using the Django template language. Django being a powerful Batteries included framework provides convenience to rendering data in a template. Django templates not only allow passing data from view to template, but also provides some
3 min read
Django Template Tags
Django Web Framework ships with dozens of tags used to implement arbitrary logics right in the template. Tags look like this: {% tag %} . Tags are more complex than variables: Some create text in the output, some control flow by performing loops or logic, and some load external information into the
7 min read
How to Add Data from Queryset into Templates in Django
In this article, we will read about how to add data from Queryset into Templates in Django Python. Data presentation logic is separated in Django MVT(Model View Templates) architecture. Django makes it easy to build web applications with dynamic content. One of the powerful features of Django is fet
3 min read
now - Django Template Tags
A Django template is a text document or a Python string marked-up using the Django template language. Django being a powerful Batteries included framework provides convenience to rendering data in a template. Django templates not only allow passing data from view to template, but also provides some
2 min read
Math Operation in Django Template
This article will guide you through how you can perform basic math operations within a Django template and best practices to handle more complex logic. Django Template Filters for Simple MathDjango templates come with Django built-in filters, which are essentially functions that transform values bef
5 min read