Restrict django FloatField to 2 decimal places

前端 未结 3 649
予麋鹿
予麋鹿 2020-12-16 14:08

I am looking for a way to limit the FloatField in Django to 2 decimal places has anyone got a clue of how this could be done without having to use a DecimalField.

I

3条回答
  •  一整个雨季
    2020-12-16 14:43

    If you are only concerned with how your FloatField appears in forms, you can use the template filter floatformat.

    From the Django Docs:

    If used with a numeric integer argument, floatformat rounds a number to that many decimal places.

    For example, if value = 34.23234, then in your template:

    {{ value|floatformat:2 }}  # outputs 34.23
    

提交回复
热议问题