Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Number.parseFloat: parsing a decimal prefix

Number.parseFloat converts its argument to text, skips leading white space, and returns the number from the longest valid decimal prefix or NaN.

Version: ECMAScript 2015 onward; example target: Node.js 24.22.2

Syntax

Number.parseFloat(string)

Number.parseFloat(value) first applies ToString, removes leading white space, and parses the longest prefix matching signed decimal syntax, an exponent, or literal Infinity. It stops at the first disallowed character, so "12.5px" becomes 12.5; if the first character cannot begin a number, the result is NaN.

The parser does not require the whole text and does not interpret other-base prefixes: Number.parseFloat("0x10") is 0, while Number("0x10") is 16. Overflow can produce an infinity and underflow a signed zero. A BigInt is first converted to text and can lose precision in Number; a Symbol throws TypeError, and an object can run conversion code.

Number.parseFloat has existed since ECMAScript 2015, refers to the same function as global parseFloat, and is not a constructor. The example deliberately reads the numeric prefix of CSS values but rejects auto with Number.isFinite. If the entire text must satisfy a format, validate it separately or use a suitably strict conversion instead of relying on where parsing stops.

Examples

const cssWidths = ["12.5px", "-3px", "auto"];
const widths = cssWidths.map(text => {
  const value = Number.parseFloat(text);
  return Number.isFinite(value) ? value : "invalid";
});
console.log(widths.join(" "));
console.log(Number.parseFloat("  -3.2e2rest"));
console.log(Number.parseFloat("0x10"), Number("0x10"));
console.log(Number.isNaN(Number.parseFloat("auto")), Number.parseFloat === parseFloat);
try { Number.parseFloat(Symbol("x")); } catch (error) { console.log(error instanceof TypeError); }

Result

12.5 -3 invalid
-320
0 16
true true
true

Number.parseFloat → CSS value prefixes, NaN, and the Number conversion difference

Sources