Number.parseFloat()
Parses a leading decimal number, including exponents, out of a string.
Number.parseFloat(string)- Added in JavaScript ES2015
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 given string, or NaN when the first non-whitespace character cannot begin a number.
Changelog
- PHP ES2015Added as a static method on Number; it is the very same function object as the global parseFloat, so Number.parseFloat === parseFloat is true.
Identical to the global parseFloat() — Number.parseFloat === parseFloat is true — and the counterpart to Number.parseInt() for values with a fractional part. There is no radix; decimal is the only base.
What it accepts
Leading whitespace, a sign, digits, one decimal point, and an exponent: ' -1.5e3xyz' parses to -1500. It also recognises 'Infinity' and '-Infinity'. Everything from the first unusable character onward is discarded, so a unit suffix costs nothing — parseFloat('12.5kg') is 12.5.
Where it differs from Number()
Number('12.5kg') is NaN, because it insists the whole string be numeric. But Number('') is 0 while parseFloat('') is NaN, and Number('0x10') is 16 while parseFloat('0x10') is 0 — it reads the zero and stops at the x. Choose based on whether trailing text is expected data or a validation failure.
Locale strings will not parse
Text produced by toLocaleString() cannot be fed back in. parseFloat('1,234.5') returns 1, stopping at the comma. Strip grouping separators, and convert the decimal mark, before parsing anything a human typed or a formatter emitted.
Example
When to use it
- Read a measurement with its unit attached, like "12.5kg"
- Convert a numeric string from a form field to a float
- Parse a value in scientific notation out of a log line
Frequently asked questions
What is the difference between parseFloat() and Number()?
parseFloat() reads a leading number and ignores trailing text, while Number() returns NaN unless the whole string is numeric. Number('') is 0 but parseFloat('') is NaN.
Why does parseFloat('1,234.5') return 1?
The comma is not part of a numeric literal, so parsing stops there. Strip grouping separators before parsing anything formatted for display.
Does Number.parseFloat() handle scientific notation?
Yes. It parses exponents, so Number.parseFloat('-1.5e3') returns -1500, unlike parseInt() which stops at the 'e'.
More examples
parseFloat versus parseInt versus Number
The three differ on exponents, hex prefixes and empty input. parseFloat() is the one that understands 1e3 while still tolerating a trailing unit.
const inputs = ['3.9kg', '1e3', '0x10', '', ' 8 '];
for (const s of inputs) {
console.log(
JSON.stringify(s),
Number.parseFloat(s),
Number.parseInt(s, 10),
Number(s),
);
}
// "3.9kg" 3.9 3 NaN
// "1e3" 1000 1 1000
// "0x10" 0 0 16
// "" NaN NaN 0
// " 8 " 8 8 8Parsing a formatted string safely
Removing the grouping commas fixes the common case, but a leading symbol still defeats the parse — strip everything before the first digit or sign as well.
function parseEnUs(text) {
const cleaned = text.replace(/,/g, '');
const n = Number.parseFloat(cleaned);
return Number.isFinite(n) ? n : null;
}
console.log(Number.parseFloat('1,234,567.89')); // 1
console.log(parseEnUs('1,234,567.89')); // 1234567.89
console.log(parseEnUs('$99.50')); // null — leading $ stops it
console.log(parseEnUs('12.5%')); // 12.5Extracting a unit and a magnitude
Because parseFloat() consumes exactly the numeric prefix, the length of its stringified result marks where the unit begins.
function split(measure) {
const value = Number.parseFloat(measure);
const unit = measure.slice(String(value).length).trim();
return { value, unit };
}
console.log(split('12.5kg')); // { value: 12.5, unit: 'kg' }
console.log(split('100 px')); // { value: 100, unit: 'px' }
console.log(split('-3.5 °C')); // { value: -3.5, unit: '°C' }
Discussion