Home > Web Front-end > JS Tutorial > Introduction to converting number to string in JavaScript_javascript skills

Introduction to converting number to string in JavaScript_javascript skills

WBOY
Release: 2016-05-16 16:22:49
Original
1400 people have browsed it

In JavaScript value type conversion, the most common conversion is between string and number values.

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 a 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 will represent the corresponding value in decimal.

Copy code 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()

The toFixed() function accepts an integer as a parameter, which specifies the exact number of digits after the decimal point. 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.


Copy code 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 a value to 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.


Copy code 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()

The 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 takes it as the precise number of digits of the value (including the integer part and the decimal part). If the If the exact number of digits is less than the number of integer parts 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.

Copy code The code is as follows:

var z = 17951.38596;
console.log(z.toPrecision(8));
console.log(z.toPrecision(3));
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template