JavaScript digital and string conversion applications

Source: Internet
Author: User
Tags tostring

This article mainly introduces JavaScript digital and string conversion applications, the need for friends can refer to the following

1. Convert numbers to Strings

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

The code is as follows:

var n = 100;

var n_as_string = n + "";

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

The code is as follows:

var string_value = string (number);

C. Use the ToString () method:

The code is as follows:

String_value = Number.tostring ();

The Number object (the basic numbers are converted to the # object so that the method can be called) The ToString () method has an optional parameter that specifies the base of the conversion. If you do not specify this argument, the conversion is based on a 10 base. However, you can also convert numbers by the number of other cardinality (between 2 and 36).

For example:

The 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.

The 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, with a specific number of digits following the decimal point.

The 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 meaningful digits, which uses exponential notation if the meaningful number of digits is not sufficient to display the entire integer portion of the number.

The code is as follows:

var n = 123456.789;

N.toprecision (4); "1.235e+5"

N.toprecision (7); "123456.8"

2. Converting strings to numbers

A. One of the missing tricks for converting a string to a number is clearly understood: the number () constructor is invoked as a function:

The code is as follows:

var number = number (string_value);

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

The 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.

The code is as follows:

Parsefloat ("3.14 meters"); Returns 3.14

D. If parseint () and parsefloat () cannot convert the specified string to numbers, they return Nan:

The code is as follows:

parseint (' eleven '); Returns Nan

Parsefloat ("$72.47"); Returns NaN

3 Method of JavaScript rounding

A. Discard the fractional part and keep the whole number of parts

parseint (5/2)

B. Rounding up, with a decimal number on the integral part plus 1

Math.ceil (5/2)

C. Rounding down

Math.floor (5/2)

D. Rounding

Math.Round (5/2)

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.