Js string to number
Many friends want to know how to convert js strings into numbers. Let's take a look together ~

There are three main methods to convert js strings into numbers: conversion function, forced type conversion and weak type conversion using js variables.

1. transfer function:

Js provides two conversion functions, parseInt () and parseFloat (). The former converts values into integers, and the latter converts values into floating-point numbers. Only when these methods are called on String type can these two functions run correctly; For other types, NaN (non-numeric) is returned.

Some examples are as follows:

parse int(" 1234 blue "); ? //Return? 1234 parse int(" 0xA "); ? //Return? 10 parse int(" 22.5 "); ? //Return? 22 parseInt ("blue"); ? //Return? Disc scone

The ParseInt () method also has a basic pattern, which can convert binary, octal, hexadecimal or any other binary string into an integer. The base is specified by the second parameter of the parseInt () method. Examples are as follows:

parseInt("AF ", 16); ? //Return? 175 parseInt(" 10 ",2); ? //Return? 2 parseInt(" 10 ",8); ? //Return? 8 parseInt(" 10 ", 10); ? //Return? 10

If the decimal number contains a leading 0, it is best to use the radix 10 to avoid accidentally getting an octal value. For example:

parse int(" 0 10 "); ? //Return? 8 parseInt("0 10 ",8); ? //Return? 8 parseInt("0 10 ", 10); ? //Return? 10

The parseFloat () method is similar to the parseInt () method.

Another difference in using the parseFloat () method is that strings must represent floating-point numbers in decimal form, and parseFloat () has no base pattern.

The following is an example of using the parseFloat () method:

parse float(" 1234 blue "); ? //Return? 1234.0 parse float(" 0xA "); ? //Return? NaN parse float(" 22.5 "); ? //Return? 22.5 parse float(" 22 . 34 . 5 "); ? //Return? 22.34 parse float(" 0908 "); ? //Return? 908 parseFloat ("blue"); ? //Return? Disc scone

2. Forced type conversion

Use type conversion to handle the types of converted values. Using a cast allows you to access a specific value, even if it is another type.

The three types of casts available in ECMAScript are as follows:

Boolean(value)- Converts a given value to a Boolean type;

Number(value)- Converts a given value into a number (either an integer or a floating-point number);

String(value)- Converts the given value into a string.

Converting a value using one of these three functions will create a new value and store the value directly converted from the original value. This will have unexpected consequences.

When the value to be converted is a string containing at least one character, a non-zero number or an object (discussed in the next section), the Boolean () function will return true. If the value is an empty string, the number 0, undefined or null, it will return false.

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

Boolean value (""); ? //False? –? Empty? The string boolean ("hi"); ? //Really? –? Not empty? String Boolean value (100); ? //Really? –? Nonzero? Numeric Boolean value (null); ? //False? -? Empty Boolean value (0); ? //False? -? Zero Boolean (new? object()); ? //Really? –? target

The conversion of Number () is similar to the parseInt () and parseFloat () methods, except that it converts the whole value instead of some values. Examples are as follows:

How come? fructification

Number (fake)? 0 (true)? 1 number (undefined)? NaN (empty)? No.0 (? "5.5?" ) No.5 (? "56?" ) No.56 (? "5.6.7?" )? South number (new? Object())NaN number (100) 100

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

var? s 1? =? String (empty); ? //"null" var? oNull? =? nullvar? s2? =? on ull . tostring(); ? //No? Work? Why? Ann? mistake

3. Weak type conversion using js variables.