The original intention of this series of articles is to “let each front-end engineer master the high frequency knowledge, for the work of power”. This is the front end of the sixth cut, I hope friends pay attention to the public number “kite”, armed with knowledge of their minds.

Js string to number there are three main ways: the use of conversion function, cast conversion, the use of JS variable weak type conversion, the following from these three aspects to illustrate the conversion mode:

6.1 Conversion Function

Js string to number conversion functions have two: parseInt, parseFloat.

  1. parseInt

The parseInt() function parses a string and returns an integer.

parseInt(string, radix)
Copy the code

Radix is an optional parameter indicating the radix of the number to be parsed. The value is between 2 and 36, beyond which NaN is returned.

console.log(parseInt('123abc'.10)); / / 123
console.log(parseInt('123abc'.2)); / / 1
console.log(parseInt('123abc'.8)); / / 83
console.log(parseInt('123abc'.16)); / / 1194684
console.log(parseInt('abc'.10)); // NaN
Copy the code
  1. parseFloat

The parseFloat() function parses a string and returns a floating point number. This function specifies whether the first character in a string is a number. If so, the string is parsed until the end of the number is reached, and then the number is returned as a number, not as a string. If the first character of the string cannot be converted to a number, parseFloat() returns NaN.

parseFloat(string)
Copy the code
console.log(parseFloat('123.4 d')); / / 123.4
console.log(parseFloat('123abc')); / / 123
console.log(parseFloat('abc')); // NaN
Copy the code

6.2 Cast Type

Js provides some cast functions. The cast function to convert a string to a Number is Number(). When Number() is called this way, it converts its argument to a Number and returns the original converted value (or NaN). (Note: the conversion is to the whole value, part of the value is a number does not complete the conversion, will return NaN)

console.log(Number('123.456')); / / 123.456
console.log(Number('123abc')); // NaN
console.log(Number("1.2.3")); // NaN
Copy the code

6.3 Weak Type conversion

  1. According to a non

Strings can be converted to integers by the bitwise non-operator (~), which first converts all values to integers, then to binary representation, and finally to bitwise inversion. (Note: this method also converts the entire value, and partial numbers do not complete the conversion.)

console.log(~~'123'); / / 123
console.log(~~'123.456'); / / 123
console.log(~~'abc'); / / 0
console.log(~~'123abc'); / / 0
console.log(~~'0'); / / 0
console.log(~~'123'); / / - 123
Copy the code
  1. The plus operator

This method also converts the entire value, and partial numeric values do not complete the conversion

console.log(+'123'); / / 123
console.log(+'123.456'); / / 123.456
console.log(+'abc'); // NaN
console.log(+'123abc'); // NaN
console.log(+'0'); / / 0
console.log(+'123'); / / - 123
Copy the code
  1. The four operators -, *, /

This method also converts the entire value, and partial numeric values do not complete the conversion

console.log('123' - 0); / / 123
console.log('123.456' * 1); / / 123.456
console.log('abc' * 1); // NaN
console.log('123abc' - 0); // NaN
console.log('0' * 1); / / 0
console.log('123' * 1); / / - 123
console.log('0XFF' / 1); / / 255
Copy the code

1. If you think this article is good, share and like it so that more people can see it

2. Pay attention to the public number of kite, and the number of the Lord together to kill the front hundred.