如何检查数字是否为小数?
我创建用于分割数字的Android应用程序。
int a,b; int result = a/b; if (result==decimal){ Log.v ("result","your result number is decimal") } else { Log.v ("result","your result number is not decimal") }
如何检查result
是否为小数?
使用模数运算符检查是否有余数。
if(a % b != 0) Log.v("result", "The result is a decimal"); else Log.v("result", "The result is an integer");
int
不会包含小数,它们总是占用结果:例如, 3 / 5 = 0
作为int
。 也就是说你可以使用modulo( %
)来确定是否删除了小数。
if(a % b > 0) { // 3 % 5 = 3 // Decimal places will be lost }
您还可以检查字符串是否包含.
。
String.parseString(decimalNumber).contains(".");