Preface
Number and Math are both built-in objects in JavaScript. Number number type is the basic data type, which we often use during the development process, including formatting of numeric precision, converting strings into numbers and other operations.
Number
Boasted attribute values
The minimum interval between two representative numbers.
Number.MAX_SAFE_INTEGER The largest safe integer in JavaScript (2^53 - 1).
Number.MAX_VALUE The maximum positive number that can be represented. The smallest negative number is -MAX_VALUE.
Number.MIN_SAFE_INTEGER The smallest safe integer in JavaScript (-(2^53 - 1)).
Number.MIN_VALUE The smallest positive number that can be represented is the positive number closest to 0 (it will not actually become 0). The largest negative number is -MIN_VALUE.
Special "non-number" values.
Number.NEGATIVE_INFINITY Special negative infinity value, which returns on overflow.
Number.POSITIVE_INFINITY A special positive infinity value that returns when overflowed.
Basic use
new Number instantiates a number object and a Number strongly converted digital string. The two values are not equal.
var value1 = new Number("123"); this.value1 = value1; (`value1 === 123 is ${value1 === 123}`); // value1 === 123 is false var value2 = Number("123"); this.value2 = value2; (`value2 === 123 is ${value2 === 123}`); // value2 === 123 is true // Determine Number type(value1 instanceof Number); (value2 instanceof Number);
Number will convert string to number
Number("123"); // 123 Number("12.3"); // 12.3 Number("12.00"); // 12 Number("123e-1"); // 12.3 Number(""); // 0 Number(null); // 0 Number("0x11"); // 17 Number("0b11"); // 3 Number("0o11"); // 9 Number("foo"); // NaN Number("100a"); // NaN Number("-Infinity"); //-Infinity
Number determines whether the number is NAN
let number1 = new Number("123"); let number2 = 123; let number3 = "string"; let number4 = null; ((number1)); // false ((number2)); // false ((number3)); // false ((number4)); // false ((NaN)); // true
Is it a finite number
((1 / 0)); // false ((10 / 5)); // true ((0 / 0)); // false
Determine whether the given parameter is an integer
function checkInteger(x, y) { if ((y / x)) { return "Integer"; } return "Non-integer"; } (checkInteger(5, 10)); // "Integer"(checkInteger(5, 11)); // "Non-integer"
Determine whether the passed parameter value is a "safe integer" (safe integer)
Remark:
The () method is used to determine whether the passed parameter value is a "safe integer".
A safe integer is an integer that meets the following conditions:
It can be accurately represented as an IEEE-754 double precision number,
Its IEEE-754 representation cannot be a result of rounding any other integer to suit the IEEE-754 representation.
For example, 2^53 - 1 is a safe integer that can be represented accurately. In any IEEE-754 rounding mode, no other integer rounding result is that integer. For comparison, 2^53 is not a safe integer. It can be represented by IEEE-754, but 2^53 + 1 cannot be represented directly using IEEE-754. In round-to-nearest and zero rounding, it will be rounded to 2^53.
The safe integer range is an integer between -(2^53 - 1) and 2^53 - 1, containing -(2^53 - 1) and 2^53 - 1.
(3); // true ((2, 53)); // false ((2, 53) - 1); // true (NaN); // false (Infinity); // false ("3"); // false (3.1); // false (3.0); // true
Convert string numbers to numeric types
Remark:
- Methods can parse a string into a floating point number. This method is the same as the global parseFloat() function and is in the ECMAScript 6 specification (for modularization of global variables).
- The given value is parsed into a floating point number. If it cannot be parsed into a floating point number, it returns NaN
function checkFloat(r) { return parseFloat(r); } (checkFloat(4.567)); // 4.567 (checkFloat("4.567abcdefgh")); // 4.567 (checkFloat("abcdefgh")); // NaN
Convert a numeric string to an integer
/** * * () * The method parses the string [value of parameter string] into an integer based on the specified radix [value of parameter radix]. * @params string: The value to be parsed. If this parameter is not a string, it is converted to a string using the ToString abstract operation. Ignore leading spaces in this parameter. * @params radix: an integer between 2 and 36, representing the cardinality of a string (the basis in a mathematical numeric system). Beware - This is not defaulted to 10. * Integer parsed from the given string. If the cardinality is less than 11 and the first non-whitespace character cannot be converted to a number, NaN is returned. * * */ function checkInt(x, base) { const parsed = parseInt(x, base); if (isNaN(parsed)) { return 0; } return parsed * 100; } (checkInt(" 0xF", 16)); // 1500 (checkInt("321", 2)); // 0
() Convert numbers into strings and return numbers with specified digits after the decimal point
var numObj = 5.123456; var n = (2); // 5.12
() Returns the string representation of the numeric object with the specified precision
var numObj = 5.123456; ("() is " + ()); // Output 5.123456("(5) is " + (5)); // Output 5.1235("(2) is " + (2)); // Output 5.1("(1) is " + (1)); // Output 5// Note: In some cases, it will be returned in exponential notation((1234.5).toPrecision(2)); // "1.2e+3"
Number Converts Date Objects
var d = new Date("December 17, 1995 03:24:00"); (Number(d));
Summarize
Among Number types, the most commonly used ones should be:
- isNaN determines whether a number is a non-digital NAN
- parseFloat converts a numeric string to a numeric type
- parseInt parse string into integer
- ToPrecision is a method of numerical, returning the specified accuracy of the numerical value, including decimal places
- toFixed returns a number with a specified decimal point accuracy, and you need to pay attention to distinguish it from toPrecision
Source code address
Code Cloud
github
The above is a detailed explanation of the use of Number Number Type in JavaScript. For more information about JavaScript Number Number Type, please follow my other related articles!