1. ParseInt (); 2. The Number (); 3.parseFloat();

Let’s introduce them one by one

1.parseInt(string, radix)

  • Parses a string and returns a decimal integer or NaN of the specified radix.
  • The value of the first parameter to be parsed. If the argument is not a number, it is converted to a number;
  • The second argument specifies the number of base numbers to be parsed
  • If the first character passed in cannot be converted to a number, parseInt returns NaN.

Here’s a compatibility question:

If the radix is undefined, 0, or unspecified, JavaScript assumes the following:

  1. If the input string begins with “0x” or “0x” (a zero followed by a lowercase or uppercase X), the radix is assumed to be 16 and the rest of the string is parsed as a hexadecimal number.
  2. If the input string begins with “0” (0), the radix is assumed to be 8 (octal) or 10 (decimal). Which radix to choose depends on the implementation. ECMAScript 5 clarifies that 10 (decimal) should be used, but not all browsers support it. Therefore, it is important to specify a radix when using parseInt.
  3. If the input string begins with any other value, the radix is 10 (decimal).

2.Number()

The function converts the value of an object to a number

  • The Number() function converts the value of an object to a Number.
  • Passing in a string through the Number() conversion function, it tries to convert it to an integer or floating-point direct. This method can only convert it based on decimal and returns NaN if non-numeric characters are present in the string.

3.parseFloat()

  • A given value is parsed to a floating point or integer, or NaN is returned if it cannot be converted to a value.
  • ParseFloat is a global function and does not belong to any object.

All direct use can be:

function circumference(r) {
  return parseFloat(r) * 2.0
}
 
console.log(circumference(3));
// Output is: 6
Copy the code

Finally, there is a SAO operation:

Write “+” before the numeric string to convert it directly

  • Male _ no. ❤; Front-end honest person, you can exchange and learn with small partners!