简体   繁体   中英

What else can i use instead of Number.isInteger?

Number.isInteger doesn't work on some IE browsers. 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 . 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)

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:

if (!value || !/^\d+$/.test(value)) {
    return false;
 } else { 
  //It's an integer
    return true;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM