trent fernandez
trent fernandez

Reputation: 339

Django: DecimalField format values displaying from db

I have a form with DecimalField:

price = forms.DecimalField(required=True, widget=forms.NumberInput(
         attrs={'class': 'form-control text-right', 
               'style': 'text-align: right', 'step': '0.00001'}))

When I displaying this field in template, it always shows values like this ex: If I have value in db is 145.00000, in template will display 145.00000 (OK). But If I have value in db is 14400.00000, in template will display 14,400.00000 (not ok) because when I set this value to another field decimal by js it will display error:

The specified value "14,400.00000" is not a valid number. 
  The value must match to the following regular expression:
   -?(\d+|\d+\.\d+|\.\d+)([eE][-+]?\d+)? 

How can I format it? I think if in Integer division have more than 3 digits it will display , instead of . my template looks like

<td>{{ form.price }}</td>

Upvotes: 1

Views: 1439

Answers (1)

Utkarsh Kaushik
Utkarsh Kaushik

Reputation: 971

You can always use str.replace() at controller level, before rendering it to the template:

decimal_number = str(decimal_number).replace(",", "")

And if you wanna do this at template level, then in Django you can disable a FloatField formatting like this:

{{ form.price|stringformat:"f" }}

Upvotes: 3

Related Questions