Integer
Documentation for the Integer type.
A whole number.
The number can be negative, zero, or positive. As Typst uses 64 bits to store integers, integers cannot be smaller than -9223372036854775808 or larger than 9223372036854775807. Integer literals are always positive, so a negative integer such as -1 is semantically the negation - of the positive literal 1. A positive integer greater than the maximum value and a negative integer less than or equal to the minimum value cannot be represented as an integer literal, and are instead parsed as a float. The minimum integer value can still be obtained through integer arithmetic.
The number can also be specified as hexadecimal, octal, or binary by starting it with a zero followed by either x, o, or b.
You can convert a value to an integer with this type's constructor.
Example
Constructor
Converts a value to an integer. Raises an error if there is an attempt to parse an invalid string or produce an integer that doesn't fit into a 64-bit signed integer.
- Booleans are converted to
0or1. - Floats and decimals are rounded to the next 64-bit integer towards zero.
- Strings are parsed in base 10 by default.
#int(
value,
base: int
) -> intParameters
Prop
Type
Methods
Calculates the sign of an integer.
- If the number is positive, returns
1. - If the number is negative, returns
-1. - If the number is zero, returns
0.
Calculates the bitwise NOT of an integer.
For the purposes of this function, the operand is treated as a signed integer of 64 bits.
Calculates the bitwise AND between two integers.
For the purposes of this function, the operands are treated as signed integers of 64 bits.
#int.bit-and(
rhs
) -> intParameters
Prop
Type
Calculates the bitwise OR between two integers.
For the purposes of this function, the operands are treated as signed integers of 64 bits.
#int.bit-or(
rhs
) -> intParameters
Prop
Type
Calculates the bitwise XOR between two integers.
For the purposes of this function, the operands are treated as signed integers of 64 bits.
#int.bit-xor(
rhs
) -> intParameters
Prop
Type
Shifts the operand's bits to the left by the specified amount.
For the purposes of this function, the operand is treated as a signed integer of 64 bits. An error will occur if the result is too large to fit in a 64-bit integer.
#int.bit-lshift(
shift
) -> intParameters
Prop
Type
Shifts the operand's bits to the right by the specified amount. Performs an arithmetic shift by default (extends the sign bit to the left, such that negative numbers stay negative), but that can be changed by the logical parameter.
For the purposes of this function, the operand is treated as a signed integer of 64 bits.
#int.bit-rshift(
shift,
logical: bool
) -> intParameters
Prop
Type
Converts bytes to an integer.
#int.from-bytes(
bytes,
endian: str,
signed: bool
) -> intParameters
Prop
Type
Converts an integer to bytes.
#int.to-bytes(
endian: str,
size: int
) -> bytesParameters
Prop
Type