检查数字是否是Java中的整数

Unk*_*ser 25 java utilities

是否有任何方法或快速方法来检查数字是否是Java中的整数(属于Z字段)?

我想可能会从四舍五入的数字中减去它,但我没有找到任何可以帮助我的方法.

我应该在哪里检查?整数Api?

小智 45

又快又脏......

if (x == (int)x)
{
   ...
}
Run Code Online (Sandbox Code Playgroud)

编辑:这是假设x已经是其他数字形式.如果您正在处理字符串,请查看Integer.parseInt.


evi*_*one 10

一个例子更多:)

double a = 1.00

if(floor(a) == a) {
   // a is an integer
} else {
   //a is not an integer.
}
Run Code Online (Sandbox Code Playgroud)

在这个例子中,可以使用ceil并具有完全相同的效果.


Dha*_*y M 5

/**
 * Check if the passed argument is an integer value.
 *
 * @param number double
 * @return true if the passed argument is an integer value.
 */
boolean isInteger(double number) {
    return number % 1 == 0;// if the modulus(remainder of the division) of the argument(number) with 1 is 0 then return true otherwise false.
}
Run Code Online (Sandbox Code Playgroud)