Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django templates: output float with all decimal places

Tags:

python

django

how can I output this number in django templates (number of decimal places is variable and I don't know it in advance):

x = 0.000015

1)

{{x}}

output is:

1,5e-05

2)

{{x|stringformat:"f"}}

output is:

0.000015

which is not localized, there should be comma! I need output to be localized and keep all decimal places.

I can't believe that django can't handle such a simple task using built-in tools.

UPDATE: I have correct settings already:

LANGUAGE_CODE = 'ru'
USE_L10N = True

And default Django output works fine for most of float numbers, it only fails on this small number. It outputs with exponent notation, but I need to output as float with all decimal places (number of decimal places is variable).

like image 514
Sergey Avatar asked Sep 07 '25 15:09

Sergey


1 Answers

If you know length of numbers you can try to use floatformat filter:

{{ x|floatformat:6 }}

where 6 is length of decimal part.

like image 103
neverwalkaloner Avatar answered Sep 09 '25 12:09

neverwalkaloner