int
Casts an expression to an integer.
int(x:number|string, base=int) -> int
Description
Section titled “Description”The int
function casts the provided value x
to an integer. Non-integer
values are truncated.
x: number|string
Section titled “x: number|string”The input to convert.
base = int
Section titled “base = int”Base (radix) to parse a string as. Can be 10
or 16
.
If 16
, the string inputs may be optionally prefixed by 0x
or 0X
, e.g.,
-0x134
.
Defaults to 10
.
Examples
Section titled “Examples”Cast a floating-point number to an integer
Section titled “Cast a floating-point number to an integer”from {x: int(4.2)}
{x: 4}
Convert a string to an integer
Section titled “Convert a string to an integer”from {x: int("42")}
{x: 42}
Parse a hexadecimal number
Section titled “Parse a hexadecimal number”from {x: int("0x42", base=16)}
{x: 66}