// The given input
String input = "99999999.99";
// We need only 2 decimals (in case of more than 2 decimals is in input)
Float value = Float.valueOf(input);
DecimalFormat df = new DecimalFormat("#0.00");
input = df.format(value);
value = new Float(input);
// Now we have a clear 2 decimal float value
// Check for overflow
value *= 100; // Multiply by 100, because we're working with cents
if (value >= Integer.MAX_VALUE) {
System.out.println("Invalid value");
}
else {
///
}
该语句不起作用,条件失败。
比较 float 与整数值的正确方法是什么?
最佳答案
你的代码没有任何问题。 99999999.99 乘以 100 等于 9999999999 。
它比 max int == 2147483647 更大(即 2^31 -1)。如果您希望能够存储更大的整数,请使用long
。
如果这不是您的问题,请进一步详细说明。
关于java - 如何知道浮点值是否大于Integer.MAX_VALUE?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41261427/