[英]What else can i use instead of Number.isInteger?
Number.isInteger doesn't work on some IE browsers. Number.isInteger 不适用于某些 IE 浏览器。 I am makin a control wheather value is integer or not.
我正在控制值是否为整数。
var decimalBasePriceKontol = Number.isInteger(BasePrice);
This is my varible.这是我的变量。
What else can i use to make work this on all browsers.我还能用什么来在所有浏览器上工作。
Thanks,谢谢,
You won't get better than the Mozilla Polyfill .你不会比Mozilla Polyfill更好。 Add this to the top of your script:
将此添加到脚本的顶部:
Number.isInteger = Number.isInteger || function(value) {
return typeof value === 'number' &&
isFinite(value) &&
Math.floor(value) === value;
};
Now, what's it doing?现在,它在做什么?
// 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;
}
Note: It works only if the value is number (integer, float, ...).注意:仅当值为数字(整数、浮点数、...)时才有效。 You may check also for other types.
您也可以检查其他类型。
You can convert it to string and then check if there is a .
您可以将其转换为字符串,然后检查是否有
.
character (decimal point).字符(小数点)。
var decimalBasePriceKontol = BasePrice.toString().indexOf(".")==-1
You can also make a replacement for Number.isInteger: (run it before the first use of Number.isInteger)您也可以替换 Number.isInteger: (在第一次使用 Number.isInteger 之前运行它)
if (!Number.isInteger) { // If Number.isInteger is not defined
Number.isInteger = function (n) {
return n.toString().indexOf(".")==-1;
};
}
To check if it's an integer, I used the below method in IE browser:要检查它是否是整数,我在 IE 浏览器中使用了以下方法:
if (!value || !/^\d+$/.test(value)) {
return false;
} else {
//It's an integer
return true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.