Atma
Atma

Reputation: 29805

how to show a rounded decimal in a decimal form field in django

I have a decimal form field. When the instance is showing I want it to show a value of 60.1 instead of 60.1111. The model field is to 4 decimal places.

I know I can change the date format like this:

class AccountPersonalInfoForm(forms.ModelForm):

    class Meta:
        model = Customer

    fields = ( 
              'amount',
              'birth_date', 
            )

def __init__(self, *args, **kwargs):
    super(AccountPersonalInfoForm, self).__init__(*args, **kwargs)
    self.fields['birth_date'].widget.format = '%B %d, %Y'
    self.fields['birth_date'].input_formats = ['%B %d, %Y']

    #can I round the decimal format here?
    self.fields['amount'].widget.format = ???

How can I round the decimal that show in a decimal field to 1 decimal place when the model field is to 4 decimal places?

Upvotes: 0

Views: 428

Answers (1)

user4179775
user4179775

Reputation:

You can limiting floats digit after the decimal point to one by using round or float("{0:.1f}".

Check:

x= 60.1111
g = float("{0:.1f}".format(x))
h = round(x, 1)

print h,g

Upvotes: 1

Related Questions