如何检查浮点数是偶数还是奇数 [英] How do I check if a float number is even or odd

查看:684
本文介绍了如何检查浮点数是偶数还是奇数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查浮点数是偶数还是奇数



例如2.2是偶数而4.3是奇数

How do I check if a float number is even or odd

for example 2.2 is even and 4.3 is odd

推荐答案

奇数甚至只适用于整数



只有整数是偶数或奇数。我们不称为分数,无理数,虚数偶数或奇数等数字





任何不是偶数的值都是奇数。



2.2不均匀 - 偶数是n = 2k形式的整数,其中k是整数。



偶数是一个整数,它可以被2整除。这意味着如果整数除以2,它就不会产生余数。





转换为整数(乘以10的幂)并尝试一下,答案可能是错误的,但这取决于你想用它们做什么。
"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.


你可以这样做:

You could do something like this:
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.


I与其他人一致,只有整数可以是偶数或奇数。就编程而言,我同意偶数是[n MOD 2 = 0]或[n%2 == 0]。但如果我试图将最右边的数字识别为偶数或奇数,我会大量使用子字符串来挑出该数字,转换为整数,然后将其检查为[数字Mod 2]。也许不要听我说,我刚开始第二学期的编程......
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...


这篇关于如何检查浮点数是偶数还是奇数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆