Django: DecimalField values displaying
11,313
You can do {{decimal_field|floatformat}}
in the template, which will round off and show ".0" only when necessary.
More reference template - floatformat
Author by
Rusty
Updated on June 08, 2022Comments
-
Rusty almost 2 years
I have a model with
DecimalField
:points_player1 = models.DecimalField(max_digits=3, decimal_places=1, blank=True, null=True)
When I displaying this field in template, it always shows values like
0.0
or1.0
etc. But I want to make this behavior more user-friendly:- If user input, for example, is
1
it displays1
(now its 1.0). - If user input is
1.5
it should displays1.5
. - By-default value is
0
, not0.0
.
What is the best way to make this? Is
DecimalField
is right choice for my case? - If user input, for example, is