Parse number

2 snippets across 2 stacks - JavaScript, Python

JSJavaScript

Parsing Numbers

JS · Numbers & Math
Syntax
Number(value)
parseInt(string, radix)
parseFloat(string)
Example
console.log(Number("42"));        // 42
console.log(Number("3.14abc"));   // NaN
console.log(parseInt("3.14abc")); // 3
console.log(parseFloat("3.14abc")); // 3.14
console.log(parseInt("ff", 16));  // 255

Note Number() is stricter -- rejects partially numeric strings. parseInt() stops at the first non-numeric character. Always pass the radix to parseInt() to avoid octal surprises.

PYPython

Numeric Type Conversion

PY · Numbers
Syntax
int(x)
float(x)
complex(real, imag)
Example
print(int("42"))
print(int(9.99))
print(float("3.14"))
print(int("0xff", 16))
Output
42
9
3.14
255

Note int() truncates toward zero (not floor). int('3.14') raises ValueError; convert to float first, then to int.

Frequently asked questions

How do you parse number?
This task is covered in 2 stacks on this page: JavaScript, Python. The "Parsing Numbers" snippet in JavaScript uses `Number(value)`.
Which code does the JavaScript example use?
The "Parsing Numbers" snippet uses `Number(value)`, from the Numbers & Math section of the JavaScript cheat sheet.
Which stacks cover "parse number" on this page?
JavaScript, Python. Together they hold 2 copy-ready snippets for this task.
Is there anything to watch out for?
Yes. For "Parsing Numbers": Number() is stricter -- rejects partially numeric strings. parseInt() stops at the first non-numeric character. Always pass the radix to parseInt() to avoid octal surprises.