Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print number with commas as thousands separator and decimal places in Python/Django

I'm trying to display numbers in my Django templates with

  1. commas as thousand sepaerator and
  2. specifying the decimal places to round to.

e.g. 76384.8739439 -> 76,384.87

Django has template tags to achieve both these.

  • intcomma - for thousand separator and
  • floatformat - for decimal places

I can't seem to find a way to do both at the same time without writing my own custom template filter. Anybody know of an easier solution?

like image 747
sizeight Avatar asked Aug 03 '11 10:08

sizeight


People also ask

How do you print a number with commas as thousands separators in Python?

You use the format specification language expression '{:,}' to convert the integer number 1000000 to a string with commas as thousand separators.

How do you make a thousands separator in Python?

Per Format Specification Mini-Language, The ',' option signals the use of a comma for a thousands separator. For a locale aware separator, use the 'n' integer presentation type instead.

How do you print comma separated numbers in Python?

Method 1: using f-stringThe inner part within the curly brackets : , says to format the number and use commas as thousand separators. Example 1: Format the number and add commas as a thousand separators to use the ',d' formatting syntax in the format() function.


1 Answers

{{ value|floatformat:2|intcomma }}
like image 144
Dogbert Avatar answered Sep 30 '22 21:09

Dogbert