Convert JavaScript strings into numbers

Source: Internet
Author: User
Tags type casting
& Amp; 65279; & amp; 65279; JavaScript strings are converted into numbers in three ways: conversion functions, forced type conversion, and weak type conversion using js variables. 1 Conversion Function: js provides two conversion functions: parseInt () and parseFloat. The former converts & amp; 20540; to an integer, and the latter returns an integer.

There are three main methods for converting JavaScript strings into numbers:

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 ");// Returns1234
ParseInt ("0xA ");// Returns10
ParseInt ("22.5 ");// Returns22
ParseInt ("blue ");// ReturnsNaN

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 );// Returns175
ParseInt ("10 ",2 );// Returns2
ParseInt ("10 ",8 );// Returns8
ParseInt ("10 ",10 );// Returns10
If the decimal number contains the leading 0, it is best to use base 10 to avoid unexpected octal values. For example:
ParseInt ("010 ");// Returns8
ParseInt ("010 ",8 );// Returns8
ParseInt ("010 ",10 );// Returns10

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 ");// Returns1234.0
ParseFloat ("0xA ");// ReturnsNaN
ParseFloat ("22.5 ");// Returns22.5
ParseFloat ("22.34.5 ");// Returns22.34
ParseFloat ("0908 ");// Returns908
ParseFloat ("blue ");// ReturnsNaN

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.
When the value to be converted is a string of at least one character, a non-zero number, or an object (This will be discussed in the next section), 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?EmptyString
Boolean ("hi ");// True?Non-emptyString
Boolean (100 );// True?Non-zeroNumber
Boolean (null );// False-Null
Boolean (0 );// False-Zero
Boolean (newObject ());// 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:

UsageResult
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 (newObject ())NaN
Number (100)100

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

VarS1=String (null );// "Null"
VarONull=Null;
VarS2=ONull. toString ();// Won'tWork,CausesAnError

3. Weak type conversion using js Variables

Let's take a small example. At first glance, we will understand.
Script
VarStr = '012. 345 ';
VarX=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.

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.