简体   繁体   English

我还能用什么来代替 Number.isInteger?

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

相关问题 if.IsInteger()在if else语句中不起作用 - Number.IsInteger() in if else statement not working 与 Number.isInteger() 苦苦挣扎 - struggling with Number.isInteger() 创建的Number.isInteger(x)无法在IE中工作 - Number.isInteger(x) which is created can not work in IE 如何在JavaScript中修复Number.isInteger()函数 - How to fix Number.isInteger() function in JavaScript 我需要检查一个数字是否不是小数,我正在尝试使用 Number.isInteger() 但不起作用 - I need to check if a number is not a decimal, I'm trying with Number.isInteger() but doesn't work 在使用 Number.isInteger 检查 object 属性是 integer 后,object 怎么可能为 null/undefined? - How can object be null/undefined after checking that object property is an integer using Number.isInteger? Number.isInteger(this) 在 Number.prototype 方法中不起作用 - Number.isInteger(this) doesn't work in Number.prototype method 使用 Number.isInteger() 方法得到错误的结果,javascript - Getting wrong result with Number.isInteger() method, javascript 在以下Javascript数组方案中,parseInt()和Number.isInteger()之间的功效差异? - Difference in efficacy between parseInt() and Number.isInteger() in following Javascript array scenario? 我还能用什么代替arguments.callee? - What else can I use instead of arguments.callee?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM