Cheap VPS host selection
Provide server host evaluation information

Detailed explanation of JavaScript parseFloat() function

parseFloat() Function is a method used in JavaScript to convert a string to a floating point number (that is, a decimal). It attempts to parse the parameter string and returns a floating point number.

parseFloat() The syntax of is as follows:

 parseFloat (string)

Among them, string Is a string parameter that needs to be converted to a floating point number.

The following is true of parseFloat() Detailed explanation and examples of functions:

  • Resolve floating point number:
  •  var num = parseFloat ( "3.14" ); console . log (num); //Output 3.14
    
  • Resolve Integer:
  •  var num = parseFloat ( "42" ); console . log (num); //Output 42
    
  • Analyze the numerical value represented by scientific counting method:
  •  var num = parseFloat ( "1.5e2" ); // 1.5 * 10^2 = 150
     console . log (num); //Output 150
    
  • Resolve values with leading zeros:
  •  var num = parseFloat ( "007" ); console . log (num); //Output 7
    
  • Parse strings containing non numeric characters:
  •  var num = parseFloat ( "3.14abc" ); console . log (num); //Output 3.14
    
  • Parse an empty string or an unresolved string:
 var num = parseFloat ( "" ); //Return NaN
 console . log (num); var num = parseFloat ( "Hello" ); //Return NaN
 console . log (num);

It should be noted that, parseFloat() It will parse as many valid digits in the string as possible. If it encounters characters that cannot be parsed, the parsing process will stop and return the parsed digits. Returns if the string starts with a non numeric character NaN (Not a Number)。

In addition, there is a related function parseInt() Used to convert a string to an integer. And parseFloat() The difference is that, parseInt() Only the integer part is parsed, and the base number can be specified.

Do not reprint without permission: Cheap VPS evaluation » Detailed explanation of JavaScript parseFloat() function