This is the 15th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021
What is the Number object
The Number object is a wrapper object for a numeric value and can be used as a constructor or utility function.
When used as a constructor, it is used to generate objects with numeric values.
var n = new Number(0);
typeof n // "object"
Copy the code
In the code above, the Number object is used as a constructor, returning an object with a value of 1.
When used as a utility function, it can convert any type of value to a numeric value.
Number(true) // 1
Copy the code
The code above converts the Boolean value true to the value 1. The use of Number as a utility function is described in the chapter Data Type Conversions.
Static attributes
The Number object has the following static properties (that is, properties defined directly on the Number object rather than on the instance).
Number.POSITIVE_INFINITY
: positive infinity, pointingInfinity
.Number.NEGATIVE_INFINITY
: negative infinity, pointing-Infinity
.Number.NaN
: indicates non-numeric, pointing toNaN
.Number.MIN_VALUE
: represents the smallest positive number (that is, the closest positive number to 0, in 64-bit floating-point systems5e-324
), correspondingly, the negative number closest to 0 is-Number.MIN_VALUE
.Number.MAX_SAFE_INTEGER
: Indicates the largest integer that can be expressed accurately, that is9007199254740991
.Number.MIN_SAFE_INTEGER
: indicates the smallest integer that can be expressed accurately, that is- 9007199254740991.
.
Number.POSITIVE_INFINITY // Infinity Number.NEGATIVE_INFINITY // -Infinity Number.NaN // NaN Number.MAX_VALUE // 1.7976931348623157e+308 number. MAX_VALUE < Infinity // true number. MIN_VALUE // 5e-324 number. MIN_VALUE > 0 // true Number.MAX_SAFE_INTEGER // 9007199254740991 Number.MIN_SAFE_INTEGER // -9007199254740991Copy the code
Instance methods
The Number object has four instance methods, all related to converting a value to the specified format.
Number.prototype.toString()
The Number object deploys its own toString method to convert a numeric value to a string.
(10).toString() // "10"
Copy the code
The toString method can take an argument that represents the base of the output. If omitted, the value is converted to decimal by default and then printed as a string. Otherwise, a number is converted to a string of some base based on the base specified by the argument.
(10).toString(2) // "1010"
(10).toString(8) // "12"
(10).toString(16) // "a"
Copy the code
In the code above, the 10 must be placed in parentheses to indicate that the dot following represents the calling object property. Without parentheses, the dot will be interpreted by the JavaScript engine as a decimal point, resulting in an error.
10.toString(2)
// SyntaxError: Unexpected token ILLEGAL
Copy the code
As long as it keeps the JavaScript engine from confusing the decimal point with the object’s dot operator, it can be written in any way. In addition to parenthesizing 10, you can also add two dots after 10. JavaScript interprets the first as a decimal point (10.0) and the second as a call to an object property to get the correct result.
10.. ToString (2) // "1010" // Other methods include 10.toString (2) // "1010" 10.0.toString(2) // "1010"Copy the code
What this really means is that you can use the toString method directly on a decimal.
10.5. The toString () / / "10.5" 10.5. ToString (2) / / "1010.1" 10.5. ToString (8) / / "12.4" 10.5. ToString (16) / / "a. 8"Copy the code
The toString method can also be called through the square bracket operator.
10['toString'](2) // "1010"
Copy the code
The toString method can only convert a decimal number to another base string. If you want to convert other base numbers back to decimal, you use the parseInt method.
Number.prototype.toFixed()
The toFixed() method converts a number to a decimal number with a specified number of digits and returns the string corresponding to that decimal number.
(10). ToFixed (2) / / "10.00" 10.005. ToFixed (2) / / "10.01"Copy the code
In the code above, 10 and 10.005 are converted to a 2-digit decimal and then to a string. The 10 must be placed in parentheses, or the following points will be treated as decimal points.
The toFixed() method takes a decimal number of arguments with a valid range of 0 to 100, beyond which a RangeError is raised.
Due to floating point numbers, rounding off the decimal 5 is uncertain and must be used with care.
(10.055). ToFixed (2) / / 10.05 (10.005). The toFixed (2) / / 10.01Copy the code
Number.prototype.toExponential()
The toExponential method is used to convert a number into scientific notation.
(10). ToExponential () / / "1 + 1 e" (10) toExponential (1) / / "is 1.0 e+1" (10). ToExponential (2) / / "1.00 e+1" (1234). ToExponential () / / "1.234 e+3" (1234). ToExponential (1) / / "is 1.2 e+3" (1234). ToExponential (2) / / "1.23 e+3"Copy the code
The toExponential method takes the number of significant digits after the decimal point and ranges from 0 to 100. Beyond this range, a RangeError will be raised.
Number.prototype.toPrecision()
Number. The prototype. The toPrecision () method is used to convert a Number to specify Number of significant digits.
(12.34). The toPrecision (1) / / "1 e + 1" (12.34). The toPrecision (2) / / "12" (12.34). ToPrecision (3) / / "12.3" (12.34). The toPrecision (4) // "12.34" (12.34).toprecision (5)Copy the code
The method takes the number of significant digits in the range 1 to 100, beyond which a RangeError is thrown.
This method is unreliable for rounding and has to do with the fact that floating-point numbers are not stored exactly.
(12.35). The toPrecision (3) / / "12.3" (12.25). The toPrecision (3) / / "12.3" (12.15). The toPrecision (3) / / "12.2" (12.45). The toPrecision (3) / / "12.4"Copy the code
Number.prototype.toLocaleString()
Number. The prototype. ToLocaleString () method takes a area code as a parameter, a string, said the current digital local written form in the region.
ToLocaleString (' zh-hans-cn-u-nu-hanidec ') // "123"Copy the code
The method can also take a second parameter configuration object, which is used to customize the return string for the specified purpose. The style property of this object specifies the output style, and the default value is Decimal, indicating that the output is in decimal form. If the value is percent, it represents the output percentage.
(123). ToLocaleString (' useful - Hans - CN, {style: 'percent}) / / "12300%"Copy the code
If the value of the style attribute is currency, then you can use the currency attribute to print the currency string in the specified format.
(123).toLocaleString('zh-Hans-CN', { style: 'currency', currency: 'CNY'}) // "CNY" (CNY).tolocalestring (' de-de ', {style: 'CNY', currency: CNY') 'was'}) / / "is 123, 00 euro" (123). ToLocaleString (' en - US, {style:' currency, currency: 'USD'}) / / "$123.00"Copy the code
If the Number. The prototype. ToLocaleString () parameter is omitted, then by the browser to decide how to deal with, usually set using the operating system. Note that this method throws an error if it uses a locale code that the browser does not know.