Example 1 — Decimal String
Parse a simple floating-point string.
Number.parseFloat("3.14"); // 3.14 How It Works
Digits and the decimal point are kept. Compare with Number.parseInt("3.14"), which returns 3.
Number.parseFloat() is a static method on Number (same API as MDN Number.parseFloat). Call Number.parseFloat(string) — not on a number instance. It turns text into a floating-point number and keeps decimals. This tutorial covers syntax, junk suffixes, scientific notation, five examples, and try-it labs.
Number only
float / NaN
decimals
global parseFloat
first bad char
Everywhere
Prices, ratings, and CSS lengths often arrive as text — "3.14", "19.99", or "4.5rem". Number.parseFloat() reads the leading floating-point number from that text.
It is the same function as the global parseFloat ((Number.parseFloat === parseFloat). The Number. form keeps number helpers together on one object.
Static methods belong to Number itself. Always write Number.parseFloat(s). There is no (5).parseFloat() on Number.prototype.
This page is part of JavaScript Number Methods. Related tools include Number.parseInt() for integers, toFixed() for display decimals, and Number.isNaN() to check failed parses.
parseFloat() MethodCall Number.parseFloat(string). The engine:
e / E)."3.14px" still yields 3.14).NaN if the first non-whitespace character cannot start a number.General form of the static method Number.parseFloat:
Number.parseFloat(string) A floating-point number, or NaN:
| Input | Returns |
|---|---|
Number.parseFloat("3.14") | 3.14 |
Number.parseFloat("3.14px") | 3.14 |
Number.parseFloat("1.23e+2") | 123 |
Number.parseFloat("4.567abcdefgh") | 4.567 |
Number.parseFloat("hello") | NaN |
NaN rather than a thrown error.Number.parseFloat("3.14"); // 3.14
Number.parseFloat("3.14px"); // 3.14
Number.parseFloat("1.23e+2"); // 123
Number.parseFloat("4.567abcdefgh"); // 4.567
Number.parseFloat("hello"); // NaN
Number.parseFloat === parseFloat; // true | Goal | Code |
|---|---|
| Decimal float from text | Number.parseFloat(s) |
| CSS size with decimals | Number.parseFloat("1.5rem") |
| Scientific notation | Number.parseFloat("1.5e3") |
| Check failed parse | Number.isNaN(Number.parseFloat(s)) |
| Integers only (truncate) | Number.parseInt(s, 10) |
| Strict whole-string number | Number(s) (not parseFloat) |
Four facts to remember about Number.parseFloat().
staticCall on Number, not instances
numberFloat or NaN
keptUnlike parseInt
parseFloatSame as the global function
Number.parseFloat() vs Number.parseInt() vs Number()Number.parseFloat(s) | Number.parseInt(s, 10) | Number(s) | |
|---|---|---|---|
"3.14px" | 3.14 | 3 | NaN |
"15.9" | 15.9 | 15 | 15.9 |
"1.23e+2" | 123 | 1 (stops at .) | 123 |
| Radix / base | No | Yes (2–36) | No |
| Global twin | parseFloat | parseInt | — |
Examples follow MDN / parseFloat patterns. Use View Output or Try It Yourself for each case.
Plain floats and strings with trailing units.
Parse a simple floating-point string.
Number.parseFloat("3.14"); // 3.14 Digits and the decimal point are kept. Compare with Number.parseInt("3.14"), which returns 3.
Useful for CSS-like values such as "3.14px" or "1.5rem".
Number.parseFloat("3.14px"); // 3.14 Parsing stops at p. Compare with Number("3.14px"), which is NaN.
Exponents, MDN-style helpers, and failed parses.
Exponent markers e / E are part of the float.
Number.parseFloat("1.23e+2"); // 123 1.23 × 102 becomes 123. Pair with toExponential() for the reverse display form.
Parse a radius string (junk allowed), guard with isNaN, then compute.
function circumference(r) {
if (Number.isNaN(Number.parseFloat(r))) {
return 0;
}
return Number.parseFloat(r) * 2.0 * Math.PI;
}
circumference("4.567abcdefgh"); // ≈ 28.695307297889173 MDN-style pattern: parse first, reject NaN, then use the float in math. "abcdefgh" would return 0.
No leading digits means NaN.
Number.parseFloat("hello"); // NaN Detect failure with Number.isNaN(): Number.isNaN(Number.parseFloat("hello")) is true.
"1.5rem" or "12.5px".Number.isNaN before multiplying (MDN circumference pattern).Number.parseFloat() Reads a StringSkip leading whitespace.
Sign, digits, decimal, optional exponent.
First invalid character ends the parse.
Success yields a number; failure yields NaN.
Number.parseFloat(s) only.Number.parseFloat === parseFloat — same built-in function.Number.parseInt.Number.parseFloat("15.9") is 15.9."1.5e3".=== NaN.Number.parseFloat() is Baseline Widely available — part of ES2015 and supported in every modern browser and Node.js.
Safe for production everywhere. Same behavior as global parseFloat, grouped under Number.
Bottom line: Use Number.parseFloat when decimals matter. Use Number.parseInt with an explicit radix for integers. Use Number() when the entire string must be a valid numeric literal.
Number.parseFloat() is the static, modular way to turn text into floating-point numbers — keeping decimals, accepting exponents, and tolerating suffixes like px.
Continue with parseInt() for integers, toFixed() for display rounding, Number.isNaN() for failed parses, or return to the Number methods hub.
Number.parseFloat(s) on the constructorNumber.isNaN(result) after parsing user inputrem, em, px)toFixed() or toPrecision() for display(5).parseFloat() — that is not a Number methodparseInt)Number())=== NaNNumber.parseFloat()Static string-to-float parsing that keeps decimals.
static
APInumber
Resultkept
Floatearly
SuffixparseFloat
SameNumber.parseFloat was added so number-related helpers live on Number, but it is literally the same function object as the global parseFloat — Number.parseFloat === parseFloat is true in modern engines.
Return to the hub for instance and static Number tutorials.
8 people found this page helpful