How to convert javascript strings into numbers and numbers into strings _ javascript tips-js tutorial

Source: Internet
Author: User
Tags type casting
This article mainly describes how to convert JavaScript strings into numbers and numbers into strings. If you need a friend, you can refer to it and hope to help you. Convert JavaScript strings to numbers

Converts a string to a number.ParseIntFunction.
ParseInt (string): The function is parsed from the string and returns an integer.


Example:
ParseInt ('20140901'): returns 123 (int );
ParseInt ('1234xxx'): 1234 (int) is returned );

If no number is parsed, a NaN value is returned and can be detected using the isNaN () function;

Example:
Var I = parseInt ('abc ');
If (isNaN (I ))
{
Alert ('nan value ');
}

The same parseFloat function converts a string to a floating point number.

Example: parseFloat ('31. 24abc'): Return 31.24;


Convert JavaScript numbers to strings


Converts a string to a number.StringClassToStringMethod

Example:
Var I = 10;
Var s = I. toString ();
Alert (typeof s); // returns String


Differences between js numbers and strings

The addition of numbers in js and the connection between strings are both + symbols. Therefore, whether it is a plus or a string connection depends on the type of the variable.

Example:
Var a = 'abc' + 'xyz'; // The value of a is abcxyz, and the string is connected to the string.
Var a = 10 + 5; // The value of a is: 15, and the number is plus.
Var a = 'abc' + 10; // The value of a is abc10. the string and number are automatically converted to a string.
Var a = 'abc' + 10 + 20 + 'cd'; // The value of a is abc1020cd.
Var a = 10 + 20 + 'abc' + 'cd'; // The value of a is 30 abccd. You can add a number first and then connect it.

Supplement:

Js string to convert numbers. There are three main methods

Conversion functions, forced type conversion, and weak type conversion using js variables.

1. conversion functions:

Js provides two conversion functions: parseInt () and parseFloat. The former converts the value to an integer, and the latter converts the value to a floating point number. The two functions can run correctly only when these methods are called for the String type. NaN (Not a Number) is returned for other types ).

Some examples are as follows:

ParseInt ("1234 blue"); // returns 1234
ParseInt ("0xA"); // returns 10
ParseInt ("22.5"); // returns 22
ParseInt ("blue"); // returns NaN

The parseInt () method also has the base mode, which can convert binary, octal, hexadecimal, or any other hexadecimal string to an integer. The base is specified by the second parameter of the parseInt () method, for example:

ParseInt ("AF", 16); // returns 175
ParseInt ("10", 2); // returns 2
ParseInt ("10", 8); // returns 8
ParseInt ("10", 10); // returns 10
If the decimal number contains the leading 0, it is best to use base 10 to avoid unexpected octal values. For example:
ParseInt ("010"); // returns 8
ParseInt ("010", 8); // returns 8
ParseInt ("010", 10); // returns 10

The parseFloat () method is similar to the parseInt () method.
Another difference between the parseFloat () method is that the string must represent a floating point number in decimal form, and parseFloat () has no base mode.

The following is an example of using parseFloat:
ParseFloat ("1234 blue"); // returns 1234.0
ParseFloat ("0xA"); // returns NaN
ParseFloat ("22.5"); // returns 22.5
ParseFloat ("22.34.5"); // returns 22.34
ParseFloat ("0908"); // returns 908
ParseFloat ("blue"); // returns NaN

2. Forced type conversion

You can also use type casting to process the type of the converted value. You can use forced type conversion to access a specific value, even if it is another type.
The three types of mandatory conversions available in ECMAScript are as follows:
Boolean (value) -- converts a given value to the Boolean type;
Number (value) -- converts a given value to a Number (which can be an integer or floating point Number );
String (value) -- converts a given value to a String.
Use one of the three functions to convert a value and store the value directly converted from the original value. This will cause unexpected consequences.
If the value to be converted is a string of at least one character, a non-zero number, or an object, the Boolean () function returns true. If the value is a null String, number 0, undefined, or null, it returns false.

You can use the following code snippet to test the forced type conversion of the Boolean type.

Boolean (""); // false-empty string
Boolean ("hi"); // true-non-empty string
Boolean (100); // true-non-zero number
Boolean (null); // false-null
Boolean (0); // false-zero
Boolean (new Object (); // true-object

The forced type conversion of Number () is similar to that of parseInt () and parseFloat (), except that it converts the entire value instead of the partial value. Example:

Result through Method
Number (false) 0
Number (true) 1
Number (undefined) NaN
Number (null) 0
Number ("5.5") 5.5
Number ("56") 56
Number ("5.6.7") NaN
Number (new Object () NaN
No. (100) 100

The last forced type conversion method String () is the simplest. The example is as follows:

Var s1 = String (null); // "null"
Var oNull = null;
Var s2 = oNull. toString (); // won't work, causes an error

3. Weak type conversion using js Variables

Let's take a small example. At first glance, we will understand.
Script
Var str = '012. 345 ';
Var x = The str-0;
X = x * 1;
Script

In the previous example, the weak type of js is used, and only arithmetic operations are performed to convert the string type to the number type. However, this method is not recommended.

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.