SoFunction
Updated on 2025-02-28

Introduction to Number Object of Javascript

The Number object is a number, its construction method:

Copy the codeThe code is as follows:
var num = 10;
var num = new Number();//num == 0
var num = new Number(value);

where value is a numeric value or a quantity that can be converted into a numeric value, such as
String '1002'
But if it is 'M122', then NaN will be returned

1. Constants, attributes

MAX_VALUE The largest number that can be represented. // 1.7976931348623157e+308
MIN_VALUE The smallest number that can be represented. // 5e-324
NaN Non-numeric value. // NaN
NEGATIVE_INFINITY Negative infinity, returns this value when overflowing. //-Infinity
POSITIVE_INFINITY It is infinity and returns this value when overflowing. //Infinity

2. The toString() method can convert a Number object into a string and return the result.

(radix);
radix optional. Specify the cardinality of the number, making an integer between 2 and 36.
If this parameter is omitted, the cardinality 10 is used.
A string representation of a number. For example, when radix is ​​2, NumberObject is converted to a string represented by a binary value.
example:

Copy the codeThe code is as follows:
var num = 10;
((2));

Output: 1010
Note: A TypeError exception is thrown when the object calling the method is not Number.

3. The toFixed() method can round Number into a number with a specified decimal number.

(num);
num Required. The number of digits specified in the specified decimal is a value between 0 and 20, including 0 and 20, and some implementations can support a larger range of values.
If this parameter is omitted, 0 will be replaced.
Return value:
No exception will be thrown between num and 20. If num > 20, an exception may be thrown.

Copy the codeThe code is as follows:
var num = new Number(13.37);
((1))

Output: 13.4

4. The toExponential() method can convert the value of the object into an exponential counting method.

This method is the legendary scientific counting method
(num)
num Required. The number of decimal places in the exponential counting method is a value between 0 and 20, including 0 and 20, and some implementations can support a larger range of values.
If this parameter is omitted, as many numbers as possible will be used.

Copy the codeThe code is as follows:
var num = new Number(10000);
((1))

Output:
1.0e+4
0 after the decimal point represents only one decimal

5. The toPrecision() method can convert the object into an exponential counting method when the value of the object exceeds the specified number of digits.

toPrecision(num), num is the specified number of bits, that is, when the number of bits exceeds, exponential counting method is used
example:

Copy the codeThe code is as follows:
var num = 10000;
((4)+'<br>');
((8));

Output:
1.000e+4//1.000 4 digits in total
10000.000//10000.000 8 digits in total

To view more JavaScript syntax, you can follow:JavaScript Reference Tutorial》、《JavaScript Code Style Guide》, I hope everyone supports me more.