js judgment passed argument is a safe integer

Because some interface parameter is of type int, so when the page acquired by js turn a bit, then found more under the median case, after several 0000 will automatically become ....

Online interpretation:

1.JS the number of only one type, i.e., 64-bit (sign bit 1bit, exponential portion of 11bits, 52bits and fractional part) of double precision floating point, integer value when too large, loss of precision occurs.

2. The so-called security i.e. integer number can be uniquely determined, i.e., 64-bit integers can be used to determine the unique binary number. Consider 253, converted to the corresponding representation decimal part comprises a total of 53, the occurrence of loss of precision, it can not distinguish between 253 and 2 ^ 53 + 1 in JS;

3. Safety and JS whether integer arithmetic can properly close, when the range of operands and results of operations are in a safe integer, in order to ensure the correct JS result of the operation.

so, when the need to digital js one more judgment;

if (Number.isSafeInteger(Math.abs(value))) {
    the console.log ( "Secure Digital" );
     // Parameter Value Display Digital 
    IF (! isNaN (value)) {
        value = value * 1
        }
    } else {
        console.log ( "not safe number" );
    }

 

Number.isSafeInteger (): determining whether the parameter value is passed a "safe integer";

Math.abs (): returns the absolute value of the int value. If the argument is not negative, the argument is returned; if the argument is negative, the opposite of the parameter is returned;

Guess you like

Origin www.cnblogs.com/changqing8023/p/12163672.html