Kotlin Numbers

Kotlin handles numbers with precision and safety. You choose the right number type based on the size and format of the value. Understanding number types prevents overflow errors and wasted memory.

Integer Types


Type    │ Size    │ Min Value             │ Max Value
────────┼─────────┼───────────────────────┼─────────────────────
Byte    │ 1 byte  │ -128                  │ 127
Short   │ 2 bytes │ -32,768               │ 32,767
Int     │ 4 bytes │ -2,147,483,648        │ 2,147,483,647
Long    │ 8 bytes │ -9.2 × 10^18          │ 9.2 × 10^18
val b: Byte  = 100
val s: Short = 30000
val i: Int   = 2_000_000
val l: Long  = 9_000_000_000L    // L suffix required

Floating-Point Types


Type    │ Size    │ Precision
────────┼─────────┼──────────────────────
Float   │ 4 bytes │ ~6-7 decimal digits
Double  │ 8 bytes │ ~15-16 decimal digits
val f: Float  = 3.14f       // f suffix required for Float
val d: Double = 3.14159265  // default decimal type

Number Literals with Underscores

Underscores inside number literals improve readability. They have no effect on the value.

val million     = 1_000_000       // easier to read than 1000000
val billion     = 1_000_000_000
val hexColor    = 0xFF5733        // hexadecimal literal
val binaryFlag  = 0b1010_1010     // binary literal

Arithmetic Operations

val a = 20
val b = 6

println(a + b)    // 26  addition
println(a - b)    // 14  subtraction
println(a * b)    // 120 multiplication
println(a / b)    // 3   integer division (no decimal)
println(a % b)    // 2   remainder (modulo)

val x = 20.0
val y = 6.0
println(x / y)    // 3.3333...  floating-point division

Integer Division Warning


┌─────────────────────────────────────────────┐
│  val result = 7 / 2                         │
│                                             │
│  Expected: 3.5                              │
│  Actual:   3  ← decimal part dropped!       │
│                                             │
│  Fix: use Double                            │
│  val result = 7.0 / 2     → 3.5             │
│  val result = 7 / 2.0     → 3.5             │
│  val result = 7.toDouble() / 2 → 3.5        │
└─────────────────────────────────────────────┘

Math Functions

import kotlin.math.*

println(abs(-15))        // 15     absolute value
println(sqrt(64.0))      // 8.0    square root
println(pow(2.0, 10.0))  // 1024.0 power
println(max(30, 45))     // 45     maximum of two values
println(min(30, 45))     // 30     minimum of two values
println(ceil(4.2))       // 5.0    round up
println(floor(4.9))      // 4.0    round down
println(round(4.5))      // 5      round to nearest

Number Conversions

val num: Int = 42

val toDouble: Double = num.toDouble()   // 42.0
val toLong:   Long   = num.toLong()     // 42L
val toFloat:  Float  = num.toFloat()    // 42.0f
val toString: String = num.toString()   // "42"

val text = "3.14"
val parsed: Double = text.toDouble()    // 3.14

Overflow Example

val max = Int.MAX_VALUE        // 2,147,483,647
println(max)                   // 2147483647
println(max + 1)               // -2147483648 ← overflow wraps around

// Fix: use Long
val bigNum = max.toLong() + 1  // 2147483648 (correct)

Practical Example: Temperature Converter

fun main() {
    val celsius = 37.0
    val fahrenheit = (celsius * 9.0 / 5.0) + 32.0
    val kelvin = celsius + 273.15

    println("Celsius    : $celsius °C")
    println("Fahrenheit : $fahrenheit °F")
    println("Kelvin     : $kelvin K")
}

Output:

Celsius    : 37.0 °C
Fahrenheit : 98.6 °F
Kelvin     : 310.15 K

Leave a Comment

Your email address will not be published. Required fields are marked *