Skip to content
Numbers and Math
step 1/5

Reading — step 1 of 5

Learn

~1 min readGetting Started

Standard arithmetic: + - * / %. Integer / truncates: 7 / 3 is 2. Float division when at least one operand is a float: 7.0 / 3 is 2.333….

val a = 17
val b = 5
println(a + b)        // 22
println(a / b)        // 3   (integer)
println(a.toDouble() / b) // 3.4
println(a % b)         // 2

Conversion is explicit — no implicit widening:

val i: Int = 5
val d: Double = i.toDouble()
val s: String = i.toString()

Kotlin numbers don't auto-promote. Use .toLong(), .toDouble(), etc. The kotlin.math package has sqrt, pow, abs, max, min, etc. — many also exist as extension methods on the numeric types.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…