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

How could I change floats which are whole numbers into integers?

When I divide the (n+1)/2 it gives me the values in float form even though they are a whole number is there anyway to turn only the floats ending in .00 into integers.Code

I tried to find whether a number was even or odd by adding 1 to the number then dividing the sum by 2 if it is an integer it would’ve been an odd number and if it had decimal places it would of been even. But after the running my code it gave me all the numbers in float form stopping me from being able to run my check for an integer as all my values where floats giving me the return of Even for all numbers.

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

>Solution :

So, to answer your question directly, if x is a float use:

if x.is_integer():
    ...

However, if you just want to know if a number is even, don’t use division! then use the mod operator:

is_even = n % 2 == 0
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