parseFloat()

parseFloat(string)
Returns: number · Added in vES1 · Updated March 14, 2026 · Global Functions
javascript parsefloat global conversion parsing

The parseFloat() function parses a string argument and returns a floating-point number. It differs from parseInt() in that it parses the entire decimal portion of the string rather than truncating at the first non-numeric character.

Syntax

parseFloat(string)

Parameters

ParameterTypeDescription
stringstringThe value to parse. If not a string, it is converted to one first. Leading whitespace is ignored.

Return Value

The parsed floating-point number. If the first character cannot be converted to a number, NaN is returned.

Examples

Basic Floating-Point Parsing

parseFloat("3.14");
// 3.14

parseFloat("  2.718  ");
// 2.718

parseFloat("3.14px");
// 3.14 (parses until invalid character)

Parsing Integer-Like Strings

parseFloat("42");
// 42 (returned as number, not integer)

parseFloat("42.0");
// 42

Precision Behavior

parseFloat("0.1 + 0.2");
// 0.1 (stops at the first non-numeric character after the decimal)

parseFloat("3.14159265358979");
// 3.14159265358979

Invalid Input

parseFloat("hello");
// NaN

parseFloat("");
// NaN (unlike Number() which returns 0)

parseFloat("   ");
// NaN

Differences from parseInt()

While both functions parse strings, they handle decimal points differently:

parseInt("3.14");
// 3 (decimal point stops parsing)

parseFloat("3.14");
// 3.14 (decimal point is included)

Also, parseInt() accepts a radix parameter while parseFloat() does not:

parseInt("ff", 16);
// 255

parseFloat("ff");
// NaN

Practical Examples

Reading CSS Values

function getPixelValue(cssValue) {
  return parseFloat(cssValue);
}

getPixelValue("100.5px");
// 100.5

getPixelValue("2.5rem");
// 2.5

Processing User Input

function sumInputs(input1, input2) {
  const num1 = parseFloat(input1);
  const num2 = parseFloat(input2);
  
  if (Number.isNaN(num1) || Number.isNaN(num2)) {
    return "Invalid number";
  }
  
  return num1 + num2;
}

sumInputs("10.5", "20.3");
// 30.8

sumInputs("abc", "5");
// "Invalid number"

See Also

  • parseInt() — Parse a string and return an integer with optional radix
  • Number.isNaN() — Check if a value is NaN with stricter semantics
  • JSON.parse() — Parse JSON strings into JavaScript values