Skip to main content

Mojo function

int

int[T: Intable](value: T) -> Int

Get the Int representation of the value.

Parameters:

  • T (Intable): The Intable type.

Args:

  • value (T): The object to get the integral representation of.

Returns:

The integral representation of the value.

int[T: IntableRaising](value: T) -> Int

Get the Int representation of the value.

Parameters:

  • T (IntableRaising): The Intable type.

Args:

  • value (T): The object to get the integral representation of.

Returns:

The integral representation of the value.

Raises:

If the type does not have an integral representation.

int(value: String, base: Int = 10) -> Int

Parses and returns the given string as an integer in the given base.

If base is set to 0, the string is parsed as an Integer literal, with the following considerations:

  • '0b' or '0B' prefix indicates binary (base 2)
  • '0o' or '0O' prefix indicates octal (base 8)
  • '0x' or '0X' prefix indicates hexadecimal (base 16)
  • Without a prefix, it's treated as decimal (base 10)

Examples: >>> int("32") 32 >>> int("FF", 16) 255 >>> int("0xFF", 0) 255 >>> int("0b1010", 0) 10

Notes: This follows Python's integer literals.

Args:

  • value (String): A string to be parsed as an integer in the given base.
  • base (Int): Base used for conversion, value must be between 2 and 36, or 0.

Returns:

An integer value that represents the string.

Raises:

If the given string cannot be parsed as an integer value or if an incorrect base is provided.

int(value: UInt) -> Int

Get the Int representation of the value.

Args:

  • value (UInt): The object to get the integral representation of.

Returns:

The integral representation of the value.