kotlin-types-numbers.md

#Numbers

Last modified: 05 May 2026

The Kotlin number types represent:

Use number types to store and process numeric data, for example, in arithmetic, counters, measurements, and other calculations.

#Choose a number type

In most cases, you can refer to the following rules to determine the correct number type for your task:

Kotlin also provides Unsigned integer types as a Beta feature.

#Integer types

Kotlin provides four integer types with different sizes and value ranges:

TypeSize (bits)Min valueMax value
Byte8-128127
Short16-32,76832,767
Int32-2,147,483,648 ($-2^{31}$)2,147,483,647 ($2^{31} - 1$)
Long64-9,223,372,036,854,775,808 ($-2^{63}$)9,223,372,036,854,775,807 ($2^{63} - 1$)

#Declare integer values

Kotlin supports the following literal forms for integer values:

Note: Kotlin does not support octal literals.

To declare a numeric value, specify the type explicitly:

kotlinval one: Int = 1

// Use underscores to improve readability
val oneBillion: Long = 1_000_000_000
val hexBytes: Int = 0xFF_EC_DE_5E
val bytes: Int = 0b11010010_01101001_10010100_10010010

val oneByte: Byte = 1
val oneShort: Short = 1

You can also append the L suffix to declare a Long value:

kotlinval oneLong = 1L

When you declare a numeric type explicitly, the compiler checks that the value fits in the range of that type:

kotlin// Value fits in Byte
val oneByte: Byte = 1

// Error: the value does not fit in Byte
val tooBig: Byte = 128

When you do not specify a numeric type, Kotlin infers Int if the value fits in the Int range. Otherwise, it infers Long:

kotlinval million = 1_000_000 // Int
val threeBillion = 3_000_000_000 // Long

If a value can be absent, use nullable types:

kotlinval maybeAbsent: Int? = null

#Floating-point types

For numbers with a fractional part, Kotlin provides Float and Double. These follow the IEEE 754 standard.

TypeSize (bits)Significant bitsExponent bitsDecimal digits
Float322486-7
Double64531115-16

#Declare floating-point values

To declare a floating-point literal, include a decimal point (.) or use exponent notation:

kotlinval pi = 3.14
val avogadro = 6.02214076e23

By default, Kotlin infers floating-point literals as Double. To declare a Float, add the f or F suffix:

kotlinval pi = 3.14 // Double
val eFloat = 2.7182818284f // Float

#Arithmetic operations

Kotlin supports standard operators: +, -, *, /, and %.

kotlinprintln(1 + 2) // 3
println(2_500_000_000L - 1L) // 2499999999
println(3.14 * 2.71) // 8.5094
println(10.0 / 3) // 3.3333333333333335

#Integer division

Division between integer values always returns an integer result (the fractional part is discarded):

kotlinval intValue = 5 / 2 // result: 2
val longValue = 5L / 2 // result: 2

To return a floating-point result, make at least one operand a Float or Double.


#Type conversion

Numeric types are not subtypes of one another. Kotlin requires explicit conversions to avoid data loss.

#Conversion functions

Example:

kotlinval intValue: Int = 1
val doubleValue = intValue.toDouble() // 1.0

#Data overflow

If the result of an operation falls outside the type's range, overflow occurs.

#Overflow in operations

kotlinval intNumber: Int = 2147483647
println(intNumber + 1) // -2147483648 (wraps around)

#Narrowing conversions

Converting a large type to a smaller one may lose data:

kotlinval large: Int = 130
val narrowed: Byte = large.toByte() // -126

#Bitwise operations

Kotlin provides bitwise operations for Int and Long using infix functions:


#Floating-point number comparison


#Boxing and caching (JVM)

On the JVM, numbers are stored as primitives unless they are nullable or used in generics (boxed). The JVM caches boxed integers from -128 to 127.