Home  >  Article  >  Web Front-end  >  How to convert numbers to strings in js?

How to convert numbers to strings in js?

青灯夜游
青灯夜游Original
2020-10-28 11:48:4540814browse

Conversion method: 1. Use the "number.toString()" statement; 2. Use the "number.toFixed(x)" statement; 3. Use the "number.toExponential(x)" statement; 4. Use "number.toPrecision(x)" statement.

How to convert numbers to strings in js?

The operating environment of this tutorial: Windows 7 system, ECMAScript version 5, Dell G3 computer.

Convert numbers into strings

①The toString() method defined by the Number class: This method can receive the conversion base (radix, the range is (between 2 and 36). If this parameter is not specified, the conversion rule will be based on decimal. For example:

②The toFixed() method defined by the Number class: This method can specify the number of digits after the decimal point. For example:

Recommended tutorial: "JavaScript Video Tutorial"

③The toExponential() method defined by the Number class: This method uses exponential Notation converts a number into an exponential form of a string with only one decimal place and the number of decimal places specified by the parameter. For example:

④The toPrecision() method defined by the Number class: This method converts a number into a string based on the specified number of significant digits. If the number of significant digits is fewer than the integer part of the number, it is converted to exponential form. For example:

Note: The value of n does not change after the above methods are called, but the corresponding string result is returned, and methods 2, 3, and 4 are There is rounding when returning results.

For more programming-related knowledge, please visit: Introduction to Programming! !

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

Statement:
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