如何检查int是否为小数? [重复]

时间:2022-10-30 14:58:55

This question already has an answer here:

这个问题在这里已有答案:

I need help with something that can help me check if an int is a decimal. Something like:

我需要帮助,以帮助我检查int是否为小数。就像是:

if(variable == decimal){ //assuming the variable was entered by a user
    System.out.println("This number is a decimal.");
} else 
    System.out.println("This number is an integer.");
}

1 个解决方案

#1


1  

If your "variable" value is a double then :

如果您的“变量”值是双倍,那么:

return Math.floor(variable) == variable;

Will return true if "variable" is an Integer.

如果“variable”是Integer,则返回true。

#1


1  

If your "variable" value is a double then :

如果您的“变量”值是双倍,那么:

return Math.floor(variable) == variable;

Will return true if "variable" is an Integer.

如果“variable”是Integer,则返回true。