The functions for converting numbers to strings include: 1. toString(), which cannot retain decimal places when converting a value to a string; 2. toFixed(), which can convert a value to a string and Display the specified number of digits after the decimal point; 3. toExponential(); 4. toPrecision().
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
1. toString()
toString() is the prototype method of the Object type. After the Number subclass inherits this method, it rewrites toString() to allow passing An integer parameter that sets 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”
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”
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”
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”
2. toFixed()
toFixed() can convert a 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”
3. toExponential()
The toExponential() method is specially used to convert numbers into strings in the form of scientific notation.
var a = 123456789; console.log(a.toExponential(2)); //返回字符串“1.23e+8” console.log(a.toExponential(4)); //返回字符串“1.2346e+8”
The parameters of the toExponential() method specify the number of decimal places to retain. Omitted parts are rounded off.
4. 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”
[Recommended learning: javascript advanced tutorial]
The above is the detailed content of What are the functions for converting javascript numbers to strings?. For more information, please follow other related articles on the PHP Chinese website!