Sling Academy
Home/FastAPI/How to format large numbers with thousand separators in Jinja template

How to format large numbers with thousand separators in Jinja template

Last updated: November 28, 2024

When working with Jinja2 templates in a Flask or Django application, you might need to display large numbers in a more readable format by including thousand separators. Fortunately, Jinja2 provides a straightforward way to achieve this using custom filters or built-in formatting tools.

Using Python's Built-in Format Method

You can use Python’s built-in string formatting capabilities to handle number formatting directly within Jinja templates.

{{ "{:,}".format(large_number) }}

In this example, {:,} is used to include commas in numbers. If large_number is 1234567, this will output 1,234,567.

Custom Jinja Filter

If you want to reuse the formatting elsewhere in your templates, you can create a custom filter. This requires modifying your Python application code.

Step 1: Define the Filter in a Python File

def format_number(value):
    """Format a number with commas as thousand separators."""
    return "{:,}".format(value)

Step 2: Register the Filter with Jinja

Next, register the filter with your Jinja environment. For example, in a Flask app:

from flask import Flask

app = Flask(__name__)
app.jinja_env.filters['format_number'] = format_number

In a Django app, you would typically add this filter to a separate file within a custom templatetags directory.

Step 3: Use the Filter in a Jinja Template

Once the filter is registered, you can use it in your templates like so:

{{ large_number|format_number }}

Conclusion

Formatting numbers with thousand separators in Jinja2 templates can significantly improve the readability of your numbers in web applications. Whether using Python's formatting options directly in your templates or implementing a custom filter, both approaches are powerful tools to enhance the user interface of your application.

Next Article: How to remove consecutive whitespace in rendered Jinja pages

Previous Article: How to format date time in Jinja templates

Series: Jinja Templates

FastAPI

You May Also Like

  • Popular useful built-in Jinja filters you should know
  • How to remove consecutive whitespace in rendered Jinja pages
  • How to format date time in Jinja templates
  • FastAPI + Jinja: How to create custom filters
  • How to pass variables from Python (FastAPI) to Jinja
  • How to decode Jinja response to string
  • How to create and use macros in Jinja
  • How to use namespace in Jinja
  • How to use if/ else in Jinja
  • How to use loops in Jinja
  • FastAPI + SQLAlchemy: Using cursor-based pagination
  • FastAPI: How to use macros in Jinja templates
  • Fixing Common Swagger UI Errors in FastAPI
  • FastAPI Error: 307 Temporary Redirect – Causes and Solutions
  • FastAPI Error: Expected UploadFile, received ‘str’
  • Resolving FastAPI ImportError: No Known Parent Package
  • FastAPI Error: No module named ‘pydantic_core._pydantic_core’
  • Resolving FastAPI 422 Error: Value is not a valid dict
  • Resolving the FastAPI Circular References Error