[英]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.