1

I am trying to show formatted number with thousands separator like '123,456,789', but with below code it doesn't show as desired at html. How should I correct this? The below does work but it shows like '123456789 EUR'

views.py

class ScatterView(TemplateView) :
    def get(self, request, *args, **kwargs) :
        context = super().get_context_data(**kwargs)
        context['price'] = str(plots.get_price()).format()
        return render(request, 'index.html', context)

index.html

{{ price }} EUR
AKN
  • 33
  • 6

1 Answers1

2

You can use the built in humanize package.

This package has several features to format numbers, dates etc. in human readable format. The one you want is the intcomma function.

Simply add 'django.contrib.humanize' to you INSTALLED_APPS, load the template tags in your template with {% load humanize %}, and use it like this:

{{ price | intcomma }} EUR

123456789 EUR will be changed to 123,456,789 EUR

Nico Griffioen
  • 5,143
  • 2
  • 27
  • 36