11

I have a FloatField in one of my model. I Add/Edit it using ModelForms. What I need is that, when I edit, the Float value stored in FloatField to be rendered with exactly 2 precision points.

i.e. if my stored value is 1000.0 or 1000.123, I want the input field of form to show initial value of 1000.00 or 1000.12.

Any suggestion on approach will be appreciated. Thank you all.

Vini.g.fer
  • 11,639
  • 16
  • 61
  • 90
simplyharsh
  • 35,488
  • 12
  • 65
  • 73
  • 1
    If you must use a float field, instead of a decimal field, see my answer here: https://stackoverflow.com/questions/23739030/restrict-django-floatfield-to-2-decimal-places/46081058#46081058 – user1847 Sep 06 '17 at 17:25

1 Answers1

21

I don't know your use case, but using DecimalField will solve your issue

from the docs: For example, to store numbers up to 999 with a resolution of 2 decimal places, you'd use:

models.DecimalField(..., max_digits=5, decimal_places=2)
user8193706
  • 2,387
  • 2
  • 8
  • 12
Ashok
  • 10,373
  • 3
  • 36
  • 23