JavaScript numeric and string conversion examples

Source: Internet
Author: User

Http://www.jb51.net/article/48465.htm

1. Converting numbers to strings

A. To convert a number to a string, simply add an empty string to it:

Copy CodeThe code is as follows:
var n = 100;
var n_as_string = n + "";


B. To make a number more explicitly converted to a string, you can use the string () function:

Copy CodeThe code is as follows:
var string_value = string (number);


C. Use the ToString () method:

Copy CodeThe code is as follows:
String_value = Number.tostring ();


The Number object (the basic numbers are converted to the numeric object so that this method can be called) The ToString () method has an optional parameter that specifies the cardinality of the transformation. If you do not specify this parameter, the conversion will be based on a base of 10. However, you can also convert numbers according to other cardinality (numbers from 2 to 36).
For example:

Copy CodeThe code is as follows:
var n = 17;
binary_string = n.tostring (2); Evaluates to "10001"
Octal_string = "0" + n.tostring (8); Evaluates to "021"
hex_string = "0x" + n.tostring (16); Evaluates to "0x11"


The D. ToFixed () method converts a number to a string and displays the specified number of digits after the decimal point. It does not use exponential notation.

Copy CodeThe code is as follows:
var n = 123456.789;
n.tofixed (0); "123457"
N.tofixed (1); "123456.79"


E. toexponential () converts a number to a string using exponential notation, preceded by a 1-digit number, and a specific number of digits after the decimal point.

Copy CodeThe code is as follows:
var n = 123456.789;
N.toexponential (1); "1.2e+5"
N.toexponential (3); "1.235e+5"


F. Toprecision () displays a number using the specified number of significant digits, and uses exponential notation if the meaningful number of digits is not sufficient to display the entire integer portion of the number.

Copy CodeThe code is as follows:
var n = 123456.789;
N.toprecision (4); "1.235e+5"
N.toprecision (7); "123456.8"


2. Convert strings to Numbers

A. A lack of skills to convert a string to a number but it is clear that the numbers () constructor is called as a function:

Copy CodeThe code is as follows:
var number = number (string_value);


B. parseint () intercepts only integers, if a string begins with "0x" or "0X", parseint () resolves it to a hexadecimal number, parseint () can even accept a parameter to specify the cardinality of the number to parse, with a valid value between 2 and 36.

Copy CodeThe code is as follows:
parseint ("3 Blind Mice"); Returns 3
parseint ("12.34"); Returns 12
parseint ("0xFF"); Returns 255
parseint ("11", 2); Returns 3 (1 * 2 + 1)
parseint ("FF", 16); Returns 255 (15 * 16 + 15)
parseint ("ZZ", 36); Returns 1295 (35 * 36 + 35)
parseint ("077", 8); Returns 63 (7 * 8 + 7)
parseint ("077", 10); Returns 77 (7 * 10 + 7)


C. parsefloat () intercepts integers and floating-point numbers.

Copy CodeThe code is as follows:
Parsefloat ("3.14 meters"); Returns 3.14


D. If parseint () and parsefloat () are not able to convert the specified string to a number, they return Nan:

Copy CodeThe code is as follows:
parseint (' eleven '); Returns Nan
Parsefloat ("$72.47"); Returns NaN


3 How JavaScript is rounded

A. Discard the fractional part, preserving the integral portion of the
parseint (5/2)

B. Rounding up, with decimals on the integer part plus 1

Math.ceil (5/2)

C. Rounding down

Math.floor (5/2)

D. Rounding

Math.Round (5/2)

JavaScript numeric and string conversion examples

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.