Syntax for creating Number object:
var myNum = new Number(value);
var myNum = Number(value);
When Number() and operator new are used together as constructors, it returns a newly created Number object. If you do not use the new operator, call Number() as a function, it will convert its own argument to a primitive value and return this value (return NaN if the conversion fails).
MAX_VALUE
The MAX_VALUE property is the largest number that can be represented in JavaScript. Its approximation is 1.7976931348623157 x 10308. The largest negative number is -MAX_VALUE.
A larger number than MAX_VALUE is Infinity. MAX_VALUE is a static property, so the call method should be Number.MAX_VALUE.
(Number.MAX_VALUE) //1.7976931348623157e+308
MIN_VALUE
The MIN_VALUE property is the smallest number that can be represented in JavaScript (near 0, but not negative). Its approximation is 5 x 10-324 。
All numbers smaller than MIN_VALUE will be converted to 0.
MIN_VALUE is a static property, so the call method should be Number.MIN_VALUE.
NaN
The NaN attribute is a special value that represents a non-numeric value. This property is used to indicate that a value is not a number. The Number object can be set to this value to indicate that it is not a numeric value.
You can use the isNaN() global function to determine whether a value is a NaN value.
is a special value that indicates that the result of some arithmetic operations (such as finding the square root of a negative number) is not a number. The methods parseInt() and parseFloat() return this value when the specified string cannot be parsed. For some functions that return a significant number in conventional cases, this method can also be used to illustrate its error situation.
JavaScript is output as NaN. Note that the results of comparing NaN with other values are always unequal, including itself. Therefore, it is not possible to compare with , to detect whether a value is a number, but to call isNaN() to compare.
Note: The global variable NaN is the same as , NaN is an unconfigurable and unmodified attribute.
(parseInt("abc")); //NaN
(NaN === NaN); //false
( === NaN); //false
(isNaN(NaN)); //true
(isNaN()); //true
NEGATIVE_INFINITY
The NEGATIVE_INFINITY attribute indicates a value less than -Number.MAX_VALUE. This value represents negative infinity.
JavaScript uses -Infinity when displaying NEGATIVE_INFINITY. The arithmetic behavior of this value is very similar to infinity. For example, the result of multiplying infinity is still infinite, and the result of dividing infinity by any number is 0.
-Infinity and Number.NEGATIVE_INFINITY are equal.
var x = (-Number.MAX_VALUE) * 2;
var y = Number.NEGATIVE_INFINITY;
(x); //-Infinity
(y); //-Infinity
(x===y); //true
POSITIVE_INFINITY
The POSITIVE_INFINITY attribute indicates a value greater than Number.MAX_VALUE. This value represents positive infinity.
JavaScript uses Infinity when displaying POSITIVE_INFINITY. The arithmetic behavior of this value is very similar to infinity. For example, the result of multiplying infinity is still infinite, and the result of dividing infinity by any number is 0.
Infinity and Number.POSITIVE_INFINITY are equal.
The isFinite() method can determine whether the parameters are finite numbers.
var x = Number.NEGATIVE_INFINITY;
var y = Number.POSITIVE_INFINITY;
var z = Infinity;
var a = "abc";
var b = 123;
(isFinite(x)); //false
(isFinite(y)); //false
(isFinite(z)); //false
(isFinite(a)); //false
(isFinite(b)); //true
toString()
The toString() method converts a Number object into a string and returns the result.
(radix)
The parameter radix is optional. Specify the cardinality of the number, making an integer between 2 and 36. If this parameter is omitted, use the cardinality 10. It is recommended to always take this parameter to prevent misunderstandings. For example, when radix is 2, NumberObject is converted to a string represented by a binary value.
A TypeError exception is thrown when the object calling the method is not Number.
var a = 100;
(()); //100
((10)); //100
((2)); //1100100
((8)); //144
((16)); //64
toLocaleString()
The toLocaleString() method converts a Number object to a string in local format.
The string representation of a number, determined by implementation, is formatted according to local specifications, may affect the punctuation marks used by the decimal point or the thousandth participle separator.
A TypeError exception is thrown when the object calling the method is not Number.
var a = 123456;
(()); //123,456
(("zh-Hans-CN-u-nu-hanidec")); //One, two, four five six
For more parameters, please refer to:MDN
toFixed()
The toFixed() method rounds Number into a number with a specified decimal number.
(num)
The parameter num is 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.
Returns the string representation of NumberObject, without exponential counting, there are fixed num digits after the decimal point. If necessary, the number will be rounded and can also be filled with 0 so that it reaches the specified length. If num is greater than le+21, the method only calls (), returning a string represented by exponential counting.
An exception is thrown when num is too small or too large. RangeError. Values between 0 and 20 will not raise this exception. Some implementations support values in a larger or smaller range. A TypeError exception is thrown when the object calling the method is not Number.
var n = 12345.6789;
(()); //12346
((2)); //12345.68
((6)); //12345.678900
((1.23e+20).toFixed(2)); //123000000000000000000.00
((1.23e-10).toFixed(2)); //0.00
Note: Due to the processing of floating point numbers, the result shown by the toFixed() method is not the so-called "rounding" or "rounding six into five doubles", but the performance of rounding four, six into five, and five is very confusing.
//In Chrome
(( 0.035 ).toFixed( 2 )); //0.04
(( 0.045 ).toFixed( 2 )); //0.04
It is recommended to write your own method to replace the default behavior of toFixed(). You can refer to:Discussion on SO:
= function(len){
var temp = (10,len);
var s = (this * temp)
return s/temp;
}
(( 0.035 ).toFixed( 2 )); //0.04
(( 0.045 ).toFixed( 2 )); //0.05
toExponential()
The toExponential() method converts the value of an object into an exponential counting method.
(num)
The parameter num is optional. 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.
Returns the string representation of NumberObject, using exponential counting method, that is, there is a digit before the decimal point and num digit after the decimal point. The decimal portion of the number will be rounded and filled with 0 if necessary so that it reaches the specified length.
An exception is thrown when num is too small or too large. RangeError. Values between 0 and 20 will not raise this exception. Some implementations support values in a larger or smaller range. A TypeError exception is thrown when the object calling the method is not Number.
var num = 10000.1234;
(()); //1.00001234e+4
((2)); //1.00e+4
((10)); //1.0000123400e+4
toPrecision()
The toPrecision() method formats the value into a string in the form of a decimal number.
(num)
The parameter num is optional. Used to control the accuracy of the numbers. This parameter is a value between 1 and 21 (and includes 1 and 21). If this parameter is omitted, the method toString() is called instead of converting the number to a decimal value.
var num = 10000.1234;
(()); //10000.1234
((2)); //1.0e+4
((10)); //10000.12340