Convert String to Floating-Point Number Method

The Convert String to Floating-Point Number method converts an alphanumeric string to a floating-point decimal number. It returns a floating-point decimal number. If it cannot convert to a number the value that the string argument contains, then it returns the following value:

NaN

For more information, see NaN Numbers.

Format

parseFloat(string)

The following table describes the arguments for the Convert String to Floating-Point Number method.

Argument Description

string

The string that this method must convert.

How the Convert String to Floating-Point Number Method Handles the String

The first character that is not a white space character must be a digit or a minus sign (-). For more information, see Use White Space to Improve Readability.

The Convert String to Floating-Point Number method does the following:

  • Ignores white space characters that occur at the beginning of the string

  • Treats the first period (.) in the string as a decimal point

  • Treats any digits that follow the first period as the fractional part of the number

  • Stops reading the string at the first nonnumeric character that occurs after the decimal point

  • Ignores the first nonnumeric character it encounters

  • Ignores all characters that occur after the first nonnumeric character

  • Converts the result into a number

Example

The following example returns a result of negative 234.37:

var num = parseFloat(" -234.37 profit");