Django: Formatting numbers with commas

2 comments

Its makes it easier to read data in a format such as "$1,000,000" instead of "$1000000".

Template tag

To use it as a template tag, first add "django.contrib.humanize" to your INSTALLED_APPS setting.

The in the template:

{% load humanize %}
{{ price|intcomma }}

Code

Or in your view code:

from django.contrib.humanize.templatetags.humanize import intcomma

return "$ %s" % intcomma(price)

[ Source ]

2 comments:

  1. n how to remove intcomma formatted numbers ??

    ReplyDelete
  2. It's just a normal string, so use replace.

    str = intcomma(price)
    original = int(str.replace(",", ""))

    ReplyDelete

Leave your thoughts ...
---
If you are having trouble with copy/pasting in comments, you need to sign in or click 'Preview'. For more information about this Firefox bug, see here.

 
Copyright © Twig's Tech Tips
Theme by BloggerThemes & TopWPThemes Sponsored by iBlogtoBlog