This feature is experimental. Please share your experiences so we know what to keep or change.
Parses the given value and returns a number.
up.util.parseNumber("123.4") // result: 123.4
The list of supported notations differs slightly from parseFloat()
and Number():
| Supported argument | Return value | |
|---|---|---|
| ✔️ Zero | "0" |
0 |
| ✔️ Positive integer | "123" |
123 |
| ✔️ Negative value | "-5" |
-5 |
| ✔️ Floating point number | "4.6" |
4.6 |
| ✔️ Numeric separators | "1_000_000" |
1000000 |
| ✔️ Already a number | 10 |
10 |
| ✔️ Negative zero | "-0" |
-0 |
If the argument cannot be parsed, it returns undefined.
This function never throws an error and never returns NaN.
| Unsupported argument | Return value | |
|---|---|---|
| ❌ Unparseable format | "foo" |
undefined |
| ❌ Missing argument | undefined |
undefined |
| ❌ Infinity | "Infinity" |
undefined |
| ❌ Exponential notation | "314e-2" |
undefined |
| ❌ Unparseable type | { foo: 3 } |
undefined |
The value to parse, usually a string.
If a number is given, it is returned without further processing.
The parsed number value.
If the argument cannot be parsed, undefined is returned.
Tip Your coding agent can search these docs locally with the Unpoly agent skill