Home >Web Front-end >JS Tutorial >How to convert number type to string type in JavaScript
This article mainly introduces the conversion of number into string in JavaScript. This article explains 4 functions for converting number into string. Friends who need it can refer to it
In JavaScript value type conversion, the most common conversion between the two types of values is string and number.
You can convert it into a string by calling the 4 functions of the number value (in fact, it is the 4 functions of the Number class after JS automatically converts the number value into the Number object) :
1.toString().
2.toFixed().
3.toExponential().
4.toPrecision().
toString()
The toString() method can be called on the number value to convert it into a string. The toString() function accepts a positive integer between 2 and 36 as a parameter, which is used to define the base number of the value; if the parameter is missing, the toString() function is expressed in decimal corresponding value.
The code is as follows:
var a = 42; console.log(a.toString(2));//101010 console.log(a.toString());//42 console.log("0x" + a.toString(16));//0x2a var b = 0xff; console.log(b.toString());//255
toFixed()
toFixed() function accepts an integer as a parameter, and the parameter functions after the specified decimal point of precise digits. The parameter accepted by the toFixed() function can also be a negative number (although it is rarely used). When the parameter is a negative number, the integer part of the value will lose precision. When using the toFixed() function to adjust values, JavaScript follows the rounding principle.
The code is as follows:
var x = 17.38; console.log(x.toFixed(0));//17 console.log(x.toFixed(1));//17.4 console.log(x.toFixed(4));//17.380 console.log(x.toFixed(-1));//20
toExponential()
You can use the toExponential() function to convert the value Expressed using scientific notation. The toExponential() function accepts a non-negative integer as a parameter (if this parameter is a negative number, a RangeError is thrown) as the precision of scientific notation. Like the toFixed() function, the toExponential() function follows the rounding principle when adjusting values.
The code is as follows:
var y = 17951.38596 console.log(y.toExponential(1));//1.8e+4 console.log(y.toExponential(0));//2e+4
toPrecision()
toPrecision() function accepts a positive integer as a parameter (if the parameter is 0 or a negative number, the program will throw a RangeError) and treat it as the exact number of digits (including the integer part and the decimal part) of the value. If the precise number of digits is less than the number of digits in the integer part of the value, the value will be converted to scientific notation. . Like the toFixed() function, the toPrecision() function follows the rounding principle when adjusting values.
The code is as follows:
var z = 17951.38596; console.log(z.toPrecision(8)); console.log(z.toPrecision(3));
The above is the detailed content of How to convert number type to string type in JavaScript. For more information, please follow other related articles on the PHP Chinese website!