The Number object is a Number. Its construction method is as follows:
Var num = 10;
Var num = new Number (); // num = 0
Var num = new Number (value );
Where value is a value or can be converted to a value, such
String '20140901'
If it is 'm122', NaN is returned.
1. constants and attributes
MAX_VALUE indicates the maximum number. // 1.7976931348623157e + 308
MIN_VALUE represents the smallest number. // 5e-324
NaN is a non-numeric value. // NaN
NEGATIVE_INFINITY is negative infinity. This value is returned when overflow occurs. //-Infinity
POSITIVE_INFINITY is positive infinity. This value is returned when overflow occurs. // Infinity
2. The toString () method converts a Number object into a string and returns the result.
NumberObject. toString (radix );
Radix is optional. Indicates the base number of a number, so that 2 ~ An integer between 36.
If this parameter is omitted, base 10 is used.
String representation of a number. For example, when radix is 2, NumberObject is converted to a string represented by a binary value.
Example:
Var num = 10;
Document. write (num. toString (2 ));
Output: 1010
Note: If the object that calls this method is not a Number, a TypeError exception is thrown.
3. The toFixed () method rounds the Number to a Number of the specified decimal places.
NumberObject. toFixed (num );
Num is required. Specifies the number of digits in a decimal place, ranging from 0 ~ Values between 20, including 0 and 20. Some implementations support a larger value range.
If this parameter is omitted, it is replaced by 0.
Return Value:
If num is between 0 and 20, no exception is thrown. If num is greater than 20, an exception may be thrown.
Var num = newnumber (13.37 );
Document. write (num. toFixed (1 ))
Output: 13.4
4. The toExponential () method converts the object value into an exponential notation.
This method is the legendary scientific notation.
NumberObject. toExponential (num)
Num is required. Number of decimal places in the specified index counting method, ranging from 0 ~ Values between 20, including 0 and 20. Some implementations support a larger value range.
If this parameter is omitted, as many numbers as possible will be used.
Var num = newnumber (10000 );
Document. write (num. toExponential (1 ))
Output:
1.0e + 4
The value 0 after the decimal point represents only one decimal place.
5. The toPrecision () method converts an object to an exponential notation when its value exceeds the specified number of digits.
ToPrecision (num), num is the specified number of digits, that is, when the number of digits exceeds the number of digits, the exponential notation is used.
Example:
Var num = 10000;
Document. write (num. toPrecision (4) + '<br> ');
Document. write (num. toPrecision (8 ));
Output:
1.000e + 4 // 1.000 4 digits in total
10000.000 // 10000.000 8 digits in total