[英]check if number is precise float (int) or actual float value
我正在做一个数学比较,以检查三个数字是否相同或两个相同。 我以为我会这样做:
var num = 5 + 5 +5;
alert(num/3);
结果显然是5.0
,如果没有一个数字,它将进入浮动状态。 但是,如何比较结果是5.0
还是5.5
?
我试过了:
if (num%1 == 0) { ... }
这给了我一个数字是否为整数,但是现在我迷路了:P
您可以使用如下形式:
if (parseInt(num) == num) { ... }
考虑使用Math.round()函数。
var num = 5 + 5 + 6;
if((num / 3) == Math.round(num / 3)) {
document.writeln("The three numbers are the same");
} else {
document.writeln("The three numbers are not the same");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.