In JavaScript, numbers and strings are converted to each other

Source: Internet
Author: User
Tags definition class definition integer numeric range string

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 Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.