Home > Web Front-end > JS Tutorial > body text

How to convert numbers to strings in javascript

青灯夜游
Release: 2023-01-07 11:47:25
Original
36619 people have browsed it

Method: 1. Use the plus sign " " operator, the syntax is "number """; 2. Use the toString() method, the syntax is "numeric variable.toString()"; 3. Use toFixed(), toExponential() or toPrecision() converts a number into a decimal format string.

How to convert numbers to strings in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

Convert numbers to strings in javascript

Method 1: Use the plus sign " " operator

When a value is added to an empty string, JavaScript will automatically convert the value to a string.

var n = 123;
n = n + "";
console.log(typeof n); //返回类型为 string
Copy after login

How to convert numbers to strings in javascript

Method 2: Use the toString() method

toString() is the prototype method of the Object type, which is inherited by Number subclasses After this method, toString() is overridden to allow passing an integer parameter to set the display mode. Numbers default to decimal display mode, and the number mode can be changed by setting parameters.

1) If the parameter is omitted, the toString() method will adopt the default mode and directly convert the number into a numeric string.

var a = 1.000;
var b = 0.0001;
var c = 1e-1;
console.log(a.toString());  //返回字符串“1”
console.log(b.toString());  //返回字符串“0.0001”
console.log(c.toString());  //返回字符串“0.0001”
Copy after login

toString() method can directly output integers and floating point numbers, retaining decimal places. Zeros at the end of the decimal place are cleared. But for scientific notation, it will be converted to a floating point number if conditions permit, otherwise the string will be output in scientific notation.

var a = 1e-14;
console.log(a.toString());  //返回字符串“1e-14”
Copy after login

By default, no matter what mode the value is expressed in, the toString() method returns a decimal numeric string. Therefore, for octal, binary or hexadecimal numbers, the toString() method will first convert them to decimal values ​​before outputting them.

var a = 010;  //八进制数值 10
var b = 0x10;  //十六进制数值10
console.log(a.toString());  //返回字符串“8”
console.log(b.toString());  //返回字符串“16”
Copy after login

2) If parameters are set, the toString() method will convert the value into the corresponding base value according to the parameters, and then output it as a string representation.

var a = 10;  //十进制数值 10
console.log(a.toString(2));  //返回二进制数字字符串“1010”
console.log(a.toString(8));  //返回八进制数字字符串“12”
console.log(a.toString(16));  //返回二进制数字字符串“a”
Copy after login

Method 3: Use the toFixed(), toExponential() or toPrecision() method to convert the number into a decimal format string

Use the toString() method to convert the number When converting to a string, decimal places cannot be preserved. This is undoubtedly inconvenient for outputting display numbers in professional fields such as currency formatting and scientific notation. For this purpose, JavaScript provides 3 dedicated methods, which are described below.

1) toFixed()

toFixed() can convert a numerical value into a string and display the specified number of digits after the decimal point.

var a = 10;
console.log(a.toFixed(2));  //返回字符串“10.00”
console.log(a.toFixed(4));  //返回字符串“10.0000”
Copy after login

2) toExponential()

The toExponential() method is specifically used to convert numbers into strings in scientific notation.

var a = 123456789;
console.log(a.toExponential(2));  //返回字符串“1.23e+8”
console.log(a.toExponential(4));  //返回字符串“1.2346e+8”
Copy after login

The parameters of the toExponential() method specify the number of decimal places to retain. Omitted parts are rounded off.

3) toPrecision()

The toPrecision() method is similar to the toExponential() method, but it can specify the number of significant digits instead of specifying the number of decimal places.

var a = 123456789;
console.log(a.toPrecision(2));  //返回字符串“1.2e+8”
console.log(a.toPrecision(4));  //返回字符串“1.235e+8”
Copy after login

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of How to convert numbers to strings in javascript. For more information, please follow other related articles on the PHP Chinese website!

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