abstract struct Int

Overview

Int is the base type of all integer types.

There are five signed integer types: Int8, Int16, Int32, Int64 and Int128, being able to represent numbers of 8, 16, 32, 64, and 128 bits respectively. There are five unsigned integer types: UInt8, UInt16, UInt32, UInt64 and `UInt128.

An integer literal is an optional + or - sign, followed by a sequence of digits and underscores, optionally followed by a suffix. If no suffix is present, the literal's type is Int32, or Int64 if the number doesn't fit into an Int32:

1 # Int32

1_i8   # Int8
1_i16  # Int16
1_i32  # Int32
1_i64  # Int64
1_i128 # Int128

1_u8   # UInt8
1_u16  # UInt16
1_u32  # UInt32
1_u64  # UInt64
1_u128 # UInt128

+10 # Int32
-20 # Int32

2147483648 # Int64

Literals without a suffix that are larger than Int64::MAX represent a UInt64 if the number fits, e.g. 9223372036854775808 and 0x80000000_00000000. This behavior is deprecated and will become an error in the future.

The underscore _ before the suffix is optional.

Underscores can be used to make some numbers more readable:

1_000_000 # better than 1000000

Binary numbers start with 0b:

0b1101 # == 13

Octal numbers start with 0o:

0o123 # == 83

Hexadecimal numbers start with 0x:

0xFE012D # == 16646445
0xfe012d # == 16646445

See Integer literals in the language reference.

Defined in:

duration.cr

Instance Method Summary

Instance Method Detail

def calendar_day #

Convenience method to represent the number of calendar days represented by this integer.


def calendar_days #

Convenience method to represent the number of calendar days represented by this integer.


def calendar_month #

Convenience method to represent the number of calendar months represented by this integer.


def calendar_months #

Convenience method to represent the number of calendar months represented by this integer.


def calendar_week #

Convenience method to represent the number of calendar weeks represented by this integer.


def calendar_weeks #

Convenience method to represent the number of calendar weeks represented by this integer.


def calendar_year #

Convenience method to represent the number of calendar years represented by this integer.


def calendar_years #

Convenience method to represent the number of calendar years represented by this integer.