Home > Net >  Django convert Decimal into integer when point value is 0
Django convert Decimal into integer when point value is 0

Time:09-17

I am trying to convert a Django Decimal field into an integer but only if it has a 0 point value.

So basically:

decimal_field = models.DecimalField(max_digits=10, decimal_places=2, default=0)

Lets say we have the following

decimal_field = 15.6 

Then when I use

if int(decimal_field):
  decimal_field = int(decimal_field)

It gets converted to 15 instead of staying 15.6 I want this to stay the same and if the decimal is 15.0 to convert to 15

CodePudding user response:

You can try this:

if decimal_field == int(decimal_field):
    decimal_field = int(decimal_field)
  • Related