[英]What else can i use instead of Number.isInteger?
Number.isInteger 不适用于某些 IE 浏览器。 我正在控制值是否为整数。
var decimalBasePriceKontol = Number.isInteger(BasePrice);
这是我的变量。
我还能用什么来在所有浏览器上工作。
谢谢,
你不会比Mozilla Polyfill更好。 将此添加到脚本的顶部:
Number.isInteger = Number.isInteger || function(value) {
return typeof value === 'number' &&
isFinite(value) &&
Math.floor(value) === value;
};
现在,它在做什么?
// This line makes sure that the function isInteger exists.
// If it doesn't it creates it
Number.isInteger = Number.isInteger || function(value) {
// This line checks to make sure we're dealing with a number object.
// After all "cat" is not an integer
return typeof value === 'number' &&
// This line makes sure we're not checking Infinity.
// Infinity is a number, and if you round it, then it equals itself.
// which means it would fail our final test.
isFinite(value) &&
// If you round, floor, or ceil an integer, the same value will return.
// if you round, floor, or ceil a float, then it will return an integer.
Math.floor(value) === value;
}
注意:仅当值为数字(整数、浮点数、...)时才有效。 您也可以检查其他类型。
您可以将其转换为字符串,然后检查是否有.
字符(小数点)。
var decimalBasePriceKontol = BasePrice.toString().indexOf(".")==-1
您也可以替换 Number.isInteger: (在第一次使用 Number.isInteger 之前运行它)
if (!Number.isInteger) { // If Number.isInteger is not defined
Number.isInteger = function (n) {
return n.toString().indexOf(".")==-1;
};
}
要检查它是否是整数,我在 IE 浏览器中使用了以下方法:
if (!value || !/^\d+$/.test(value)) {
return false;
} else {
//It's an integer
return true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.