# In JavaScript, numbers and strings are converted to each other

Source: Internet
Author: User

A. Convert numbers into strings

①number class definition of ToString () method: This method can receive an optional parameter representing the conversion cardinality (radix, range between 2~36), and if this argument is not specified, the conversion rule will be based on decimal. For example:

②number class-Defined tofixed () method: This method can specify the number of digits after the decimal point. For example:

The Toexponential () method of the ③number class definition: This method uses exponential notation to convert a number to an exponential string, with only one digit before the decimal point and the number of digits after the decimal point specified by the parameter. For example:

④number class-Defined Toprecision () method: This method converts a number to a string based on a specified number of significant digits. If the number of digits in a valid number is less than the number of digits in the integer part of the number, it is converted to exponential form. For example:

Note: These methods do not change the value of n after the call, only the corresponding string results are returned, and the 2, 3, 4 methods are rounded when the result is returned.

Two. converting strings to numbers

① passes a number () conversion function to a string that attempts to convert it to an integer or floating-point number direct, a method that can only be converted based on decimal and cannot have non-numeric characters in the string, or it will return Nan.

②parseint () function: It is a global function, does not belong to a method of any class, and resolves only integers. If the string prefix is "0x" or "0X", parseint () interprets it as a hexadecimal number. When parsing, it skips any number of leading spaces, resolves as many numeric characters as possible, and ignores the following, and returns Nan if the first non-whitespace character is a non-numeric character. For example:

parseint () can also receive a second optional parameter that specifies the cardinality of the numeric conversion, and the legal value range is 2~36, for example:

③parsefloat () function: It is also a global function and is not subordinate to any class of methods that can parse integers and floating-point numbers. It does not recognize the hexadecimal prefix "0x" or "0X". It also skips any number of leading spaces when parsing, resolves more numeric characters as much as possible, ignores the following, and returns Nan if the first non-whitespace character is a non-numeric character. For example:

Related Keywords:
Related Article ## Cloud Intelligence Leading the Digital Future

Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

Register Now > ## 11.11 Big Sale for Cloud

Get Unbeatable Offers with up to 90% Off，Oct.24-Nov.13 (UTC+8)

Get It Now > ## Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth \$300-1200 USD