Django convert Decimal into integer when point value is 0


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

Asked By: Rob



You can try this:

if decimal_field == int(decimal_field):
    decimal_field = int(decimal_field)
Answered By: Inozem
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.