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.