Converting numbers to strings in JavaScript

Source: Internet
Author: User

I. Converting numbers into strings

The ToString () method defined by the ①number class: This method can receive an optional parameter that represents the conversion cardinality (radix, range between 2~36), and 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:

The toexponential () method defined by the ③number class: This method converts a number to an exponential string using exponential notation, where there is only one digit before the decimal point, and the number of digits after the decimal point is specified by the parameter. For example:

Toprecision () method defined by the ④number class: This method converts a number to a string based on the 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, it is converted to an exponential form. For example:

Note: None of the above methods have changed after the call N, only the corresponding string result is returned, and the 2, 3, and 4 methods are rounded when the result is returned.

Two. Convert strings to Numbers

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

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

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

③parsefloat () function: It is also a global function, not from a method belonging to any class, which resolves integers and floating-point numbers. It does not recognize the hexadecimal prefix "0x" or "0X". It also parses any number of leading spaces, parses as many numeric characters as possible, and ignores the subsequent contents, and returns Nan if the first non-whitespace character is not a numeric character. For example:

Converting numbers to strings in JavaScript

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.