Skip to content

Int

Built-in type · Written 42, -7, or 1_000_000

An Int is a whole number. Use one for counting, positions in a list, scores, years, and anything else that is never a fraction; use Float when it can be.

Underscores can group the digits of a long number, so 1_000_000 is one million. An Int holds any whole number from -9223372036854775808 to 9223372036854775807, which is more than nine quintillion either way. A calculation that would go past those limits raises an error rather than giving a wrong answer.

const lives = 3
const population = 8_100_000_000
const next_year = 2026 + 1 # → 2027
print(lives * 2) # → 6
print(17 // 5, 17 % 5) # → 3 2

Dividing with / always gives a Float, even for two Ints: 7 / 2 is 3.5. To divide and keep a whole number, use //, and % for what is left over. Wherever a Float is expected, an Int is accepted and becomes one.

Counting
times { ... } Runs a block that many times
up_to(target) Counts up to a number
down_to(target) Counts down to a number
Checking
even?(), odd?() Whether it is even or odd
multiple_of?(divisor) Whether it divides evenly
zero?(), positive?(), negative?() Its sign
Comparing and limiting
abs() The distance from zero
clamp(minimum, maximum) Kept within a range
between?(minimum, maximum) Whether it is within a range
Showing as text
to_string() The same text print shows, or another base
format(...) With digit grouping
Number theory
digits() Its decimal digits
gcd(other), lcm(other) Greatest common divisor, least common multiple
factorial() The product of every whole number up to it
Converting
to_float() The same number as a Float
Operator Meaning Example
+ - * Add, subtract, multiply 10 - 3 * 2 → 4
/ Divide, giving a Float 7 / 2 → 3.5
// Divide, then round down 7 // 2 → 3
% The remainder after // 7 % 3 → 1
** Raise to a power 2 ** 10 → 1024
== != < <= > >= Compare 3 < 5 → true

// always rounds down, toward negative numbers, so -7 // 2 is -4, and % gives the matching remainder: -7 % 3 is 2. A negative power, such as 2 ** -1, raises an error, because its answer is a fraction; write 2.0 ** -1 to get 0.5. Dividing by zero raises an error.

times { index: Int => ... }

Runs the block this many times, passing it 0, then 1, and so on, up to one less than the number.

3.times { index =>
print("Turn #{index}")
}
# prints Turn 0, Turn 1, Turn 2

Raises when the number is negative.

up_to(target: Int): Range

A Range from this number up to target, including both. It is the same as writing number..target.

1.up_to(4) # → 1..4
for floor in 1.up_to(3) {
print("Floor #{floor}")
}
# prints Floor 1, Floor 2, Floor 3

When target is smaller, the range is empty. If both numbers are written out, Emerald points this out before the program runs and suggests down_to.

up_to(target: Int) { value: Int => ... }

Counts up to target right away, running the block with each number.

2.up_to(4) { value =>
print(value)
}
# prints 2, 3, 4

down_to(target: Int): Range

A Range from this number down to target, including both.

for count in 3.down_to(1) {
print(count)
}
print("Liftoff!")
# prints 3, 2, 1, Liftoff!

When target is larger, the range is empty.

down_to(target: Int) { value: Int => ... }

Counts down to target right away, running the block with each number.

3.down_to(1) { value =>
print(value)
}
# prints 3, 2, 1

even?(): Bool

Whether the number is even.

4.even?() # → true
7.even?() # → false

odd?(): Bool

Whether the number is odd.

7.odd?() # → true

multiple_of?(divisor: Int): Bool

Whether the number divides evenly by divisor, leaving nothing over.

12.multiple_of?(4) # → true
12.multiple_of?(5) # → false

Raises when divisor is zero.

zero?(): Bool

Whether the number is zero.

0.zero?() # → true

positive?(): Bool

Whether the number is greater than zero.

3.positive?() # → true
0.positive?() # → false

negative?(): Bool

Whether the number is less than zero.

-3.negative?() # → true

abs(): Int

The distance from zero: the number without its sign.

-5.abs() # → 5

Raises for the smallest Int, -9223372036854775808, whose distance from zero is one too large to be an Int.

clamp(minimum: Int, maximum: Int): Int

The number, kept within minimum to maximum. A number below the range becomes minimum, and a number above it becomes maximum.

15.clamp(0, 10) # → 10
-3.clamp(0, 10) # → 0
7.clamp(0, 10) # → 7

Raises when minimum is greater than maximum.

between?(minimum: Int, maximum: Int): Bool

Whether the number is from minimum to maximum, including both.

7.between?(1, 10) # → true
10.between?(1, 10) # → true
11.between?(1, 10) # → false

Raises when minimum is greater than maximum.

to_string(base: Int = 10): String

The number as text, exactly as print shows it. With a base from 2 to 36, it is written in that base instead, using the digits 0 to 9 and then the letters a to z.

42.to_string() # → "42"
255.to_string(base: 16) # → "ff"
5.to_string(base: 2) # → "101"

Raises when base is not from 2 to 36.

format(group_digits: Bool = false): String

The number as text. With group_digits: true, a comma separates every three digits.

1234567.format(group_digits: true) # → "1,234,567"

digits(): List[Int]

The number’s decimal digits, from left to right. A negative number gives the same digits as its positive twin.

2048.digits() # → [2, 0, 4, 8]
0.digits() # → [0]

gcd(other: Int): Int

The greatest common divisor: the largest number that divides both this number and other evenly.

12.gcd(18) # → 6

Raises when the answer is too large to be an Int, which only very large numbers reach.

lcm(other: Int): Int

The least common multiple: the smallest positive number that both this number and other divide evenly. It is 0 when either number is 0.

4.lcm(6) # → 12

Raises when the answer is too large to be an Int.

factorial(): Int

The product of every whole number from 1 up to this one. The factorial of 0 is 1.

5.factorial() # → 120
0.factorial() # → 1

Raises when the number is negative, or 21 or more, whose factorial is too large to be an Int.

to_float(): Float

The same number as a Float. You rarely need it: an Int becomes a Float on its own wherever a Float is expected.

3.to_float() # → 3.0

An Int can also come from text, or from a Float by rounding it:

"42".to_int() # → 42
"4.5".to_int_maybe() # → nothing
"abc".to_int_or(0) # → 0
2.7.round() # → 3

See String and Float for these methods.