parseFloat()
Parses a string into a floating-point number, reading as far as the numeric syntax allows.
parseFloat(string)- Added in JavaScript ECMAScript 1 (1997)
Parameters
| Parameter | Type | Description |
|---|---|---|
$string | string | The value to parse, coerced to a string. Leading whitespace is ignored. |
Return value
numberA floating-point number parsed from the string, or NaN when the first non-whitespace character cannot begin a number.
parseFloat() reads a decimal numeral off the front of a string and ignores the rest, the same lenient contract as parseInt() but with a wider alphabet. It accepts a sign, digits, one decimal point, one exponent marker (e or E) with at least one digit before it, and the literal Infinity.
When to reach for it
Use it when the input is a number followed by units or noise you want to discard — "1.5rem", "12.75 kg", a measurement scraped from text. If the whole string is supposed to be numeric, Number() is the better tool because it fails loudly on trailing characters.
What surprises people
There is no hexadecimal support: parseFloat("0x10") is 0, because parsing stops at the x. A second decimal point ends the parse, so "1.2.3" yields 1.2. Values beyond Number.MAX_VALUE come back as Infinity rather than throwing. And despite the name, there is no separate float type in JavaScript — parseFloat("42") and parseInt("42") return the identical value.
Because the result is an IEEE 754 double, digits past roughly 17 significant figures are lost. Parse currency as integer cents, or keep the original string, if exactness matters.
Example
When to use it
- Strip the unit off a CSS length such as "1.5rem" or "12.5%".
- Read a decimal measurement out of a line of free text.
- Convert a numeric field from a CSV or form entry that may carry trailing whitespace.
Frequently asked questions
What is the difference between parseFloat and parseInt?
parseFloat accepts a decimal point and an exponent, while parseInt stops at the first character that is not a digit in the chosen base. parseFloat("3.9") is 3.9; parseInt("3.9", 10) is 3.
Why does parseFloat return NaN for an empty string?
Because there is no numeric character to read, and the function returns NaN whenever the first non-whitespace character cannot start a number. Number("") returns 0 instead, which is a common source of disagreement between the two.
How do I make parseFloat reject strings with trailing characters?
Use Number() or a regular expression test instead, since parseFloat is defined to ignore everything after the numeral. Number("12.5kg") returns NaN, which is what strict validation wants.
More examples
Pulling units off a value
Prints { n: 1.5, unit: "rem" }, { n: 100, unit: "%" } and { n: -12.5, unit: "px" }. parseFloat finds the numeric prefix; the remainder of the string is the unit.
function splitUnit(value) {
const n = parseFloat(value);
const unit = String(value).slice(String(n).length).trim();
return { n, unit };
}
console.log(splitUnit("1.5rem"));
console.log(splitUnit("100%"));
console.log(splitUnit("-12.5 px"));parseFloat versus Number
Number is strict and returns NaN for "12.5kg" but 0 for "" and " "; parseFloat salvages 12.5 from "12.5kg" but returns NaN for empty input. They also disagree on "0x10": 0 versus 16.
const inputs = ["12.5", "12.5kg", "", " ", "1e3", "0x10"];
for (const s of inputs) {
console.log(JSON.stringify(s), parseFloat(s), Number(s));
}Precision limits
The result is an IEEE 754 double, so an integer above 2**53 is rounded, tiny exponents flush to zero, and huge ones saturate to Infinity instead of throwing.
console.log(parseFloat("0.1") + parseFloat("0.2")); // 0.30000000000000004
console.log(parseFloat("900719925474099.1")); // 900719925474099.1
console.log(parseFloat("9007199254740993")); // 9007199254740992
console.log(parseFloat("1e-400")); // 0 — underflow
console.log(parseFloat("1e400")); // Infinity
Discussion