Skip to main content

04 - Integers

Motoko's main number types are Int, Nat and Float.

Nat

Nats are "whole" positive numbers. By default positive whole nunbers are casted to type Nat.

1 // 1 : Nat
2 // 2: Nat

Large Nat numbers could be written with underscore as divider:

123_456_789 // 123_456_789 : Nat

Even when you write a large number without underscores, Motoko will add them during the print out.

123456789 // 123_456_789 : Nat

Hexadecimal numbers begin with 0x:

0xF // 15 : Nat

Ints

Integers represent whole numbers that could be either positive or negative.

+3 // 3 : Int
-3 // -3 : Int

Every positive number literal is treated as Nat. However, it doesn't cause a problem in arithmetic operations since Nat is subtype of Int.

Arithmetic operations

Motoko has several operators that work with Nat and Int values.

1 + 1 // => 2 : Nat
+2 - 5 // => -3 : Int
3 * 3 // => 9 : Nat

Integer division produces integer results:

5 / 2 // => 2 : Nat

% operator performs modular division:

5 % 2 // => 1 : Nat

Integers support comparison:

2 > 1  // => true
2 < 1  // => false
2 >= 1 // => true
2 <= 1 // => false