Click here to Skip to main content
15,901,205 members
Please Sign up or sign in to vote.
2.00/5 (4 votes)
See more:
How do I check if a float number is even or odd

for example 2.2 is even and 4.3 is odd
Posted
Updated 4-Feb-17 15:52pm
Comments
ridoy 1-Jan-14 1:29am    
certainly 2.2 is not an even nor 4.3 is an odd!

"odd and even only applies to integers"

"Only integers are even or odd. We do not call fractions, irrational numbers, imaginary numbers, and so on either even or odd"


Any value that is not an even integer is odd.

2.2 is not even -- "An even number is an integer of the form n=2k, where k is an integer."

"An even number is an integer which is "evenly divisible" by two. This means that if the integer is divided by 2, it yields no remainder."


Convert to an integer (by multiplying by a power of ten) and try it, the answer may be wrong, but it depends on what you want to do with them.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 31-Dec-13 20:14pm    
Answered, my 5.
—SA
[no name] 31-Dec-13 20:33pm    
You have given a good answer to a question that has no meaning.
ridoy 1-Jan-14 1:30am    
+5
You could do something like this:
C
isEven = (num * 10) % 2 == 0;

That only works if you only care about the first digit, depends on how many decimal places you want to go out to.
 
Share this answer
 
v3
I agree with the others, only an integer can be "even" or "odd". As far as programming goes, I agree that an even number is [n MOD 2 = 0] or [n % 2 == 0]. But if I were trying to identify the furthest-right digit as "even" or "odd", I'd prolly use a substring to single out that digit, convert to integer, then check it as [digit Mod 2]. Maybe don't listen to me, though, I only just started my second semester programming...
 
Share this answer
 
Comments
Richard MacCutchan 5-Feb-17 3:06am    
Three years too late.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900