Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

You can try this:

if decimal_field == int(decimal_field):
    decimal_field = int(decimal_field)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading