#Numbers
Last modified: 05 May 2026
The Kotlin number types represent:
- Integer values (Byte, Short, Int, and Long)
- Floating-point values (Float and Double)
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:
- Use
Intfor whole numbers. - Use
Longfor whole numbers outside theIntrange. - Use
Doublefor decimal numbers. - Use
Floatwhen lower precision is acceptable or required. - Use
ByteandShortwhen an API or data format requires them.
Kotlin also provides Unsigned integer types as a Beta feature.
#Integer types
Kotlin provides four integer types with different sizes and value ranges:
| Type | Size (bits) | Min value | Max value |
|---|---|---|---|
| Byte | 8 | -128 | 127 |
| Short | 16 | -32,768 | 32,767 |
| Int | 32 | -2,147,483,648 ($-2^{31}$) | 2,147,483,647 ($2^{31} - 1$) |
| Long | 64 | -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:
- Decimals:
123 - Hexadecimals:
0x0F - Binaries:
0b00001011
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.
| Type | Size (bits) | Significant bits | Exponent bits | Decimal digits |
|---|---|---|---|---|
| Float | 32 | 24 | 8 | 6-7 |
| Double | 64 | 53 | 11 | 15-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
toByte()toShort()toInt()toLong()toFloat()toDouble()
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:
shl(bits)– signed shift leftshr(bits)– signed shift rightushr(bits)– unsigned shift rightand(bits)– bitwise ANDor(bits)– bitwise ORxor(bits)– bitwise XORinv()– bitwise inversion
#Floating-point number comparison
- Static types: Follows IEEE 754 (e.g.,
NaN == NaNisfalse). - Generic/Nullable types: Uses
equals()andcompareTo(). NaNis equal to itself.NaNis greater thanPOSITIVE_INFINITY.-0.0is less than0.0.
#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.
- Within cache range:
===(referential equality) may betrue. - Outside cache range:
===will befalse. - Always use
==for structural equality when comparing numeric values.