Kotlin Numbers (Integers) & Usage Examples | Android Introduction #8
In this reading, you will learn more about the four basic number types in Kotlin: Int, Long, Double and Float. You will also learn how to carry out essential math operations on these number types.
Numbers in Kotlin
In Kotlin, these are the four basic types representing numbers:
- Int is the default integer number representation when a whole number (such as 20) is specified.
- Long is an integer representation supporting larger numbers. You create it by using an L suffix.
- Double is the default decimal number representation. It holds values up to 15 or 16 decimal places.
- Float is an integer representation supporting smaller decimal numbers. It holds values up to 7 or 8 decimal places. You create it using the F or f suffix.
fun main() {
val a = 10 // the type of a is Int
val b = 2147483647L // the type of b is Long
val c = 10.0 // the type of c is Double
val d = 10F // the type of d is Float
}
All numbers can be negative, and you can use underscores (‘_’) inside longer numbers to improve their readability.
fun main() {
val smallDebt = -0.72
println(smallDebt) // -0.72
val million = 1_000_000
println(million) // 1000000
}
You can transform one number type into another using transformation functions. They start with the prefix to and then the type you want. For instance, to transform Int or Long into Double, use toDouble() as in the following example.
fun main() {
val i: Int = 10
val l: Long = i.toLong()
val d1: Double = i.toDouble()
val d2: Double = l.toDouble()
println(d1) // 10.0
println(d2) // 10.0
}
With all numbers, you can do standard math operations like addition, subtraction, division or multiplication.
fun main() {
println(2 + 3) // 5
println(2 - 3) // -1
println(2 * 3) // 6
println(8 / 2) // 4
println(0.5 * 4.5) // 2.25
println(2.5 / 0.5) // 5.0
}
There is also an operation called remainder, represented by the following symbol: %. Remainder returns what would be left if you divide an integer by another integer.
fun main() {
println(10 % 3) // 1
println(11 % 3) // 2
println(12 % 3) // 0
println(13 % 3) // 1
// The sign of the result is always the same as the sign of the left side
println(-1 % 3) // -1
println(1 % -3) // 1
println(-1 % -3) // -1
}
When you carry out an operation between integer values, the result is an integer. If you conduct an operation between Int and Long, the result is Long. When you conduct operations between a decimal type and either an integer or a decimal type, the result is the decimal type. If you carry out an operation between Float and Double, the result is Double.
fun main() {
val a = 1 + 2L // the type of a is Long
val b = 1 + 2.0 // the type of b is Double
val c = 1.0F + 2 // the type of c is Float
val d = 1.0F + 2.0 // the type of c is Double
}
But take care whenever you divide two integers because the result of this operation will be an integer. If the calculation results in a decimal, the decimal part of this operation is lost. To ensure fractions are retained, transform one of the values to a Double or Float before the math operation.
fun main() {
val a = 5
val b = 2
println(a / b) // 2
println(a.toDouble() / b) // 2.5
}
Operational precedence is a math concept that refers to the sequence in which a calculation is evaluated. Operational precedence is respected in all Kotlin math operations. For example, 1 plus 2 times 3 is 7, not 9, because multiplication is always done before addition.
fun main() {
println(1 + 2 * 3) // gives a result of 7
}
Where necessary, you can dictate the order of precedence by using parentheses. In our amended example, the precedence is changed by putting the 1 + 2 part of the calculation into parentheses. This way, the addition is done before the multiplication, producing a result of 9.
fun main() {
println((1 + 2) * 3) // gives a result of 9
}
Remember that depending on the method used to calculate the desired value, the outcome can be very different. Consider a scenario where you are working on a small game project. You build some code to keep track of a player’s score and bonuses through the levels of the game. Then you need to calculate their final score. The formula you create is: score1 + score2 * bonusScore = totalScore
The correct way to score the game is to add score1 and score2 together and then multiply the result by the bonus score. However, if you put that calculation into Kotlin, the multiplication is done first, then the addition, resulting in an incorrect player’s final score.
To ensure the correct score is recorded you must override the built-in precedence by surrounding the calculation you want to perform first with parentheses. Your amended equation is: (score1 + score2) * bonusScore
These examples are basic, but they do indicate how careful you need to be when dealing with different values and different math operations. Always check that your math is calculated the way you intend. You can guarantee this by using parentheses to force the order of execution.
Augmented assignments
Variables defined with var can have their value changed.
fun main() {
var a = 10
println(a) // 10
a = 20
println(a) // 20
}
This is often used to add or subtract something from the value.
fun main() {
var a = 10
println(a) // 10
a = a + 10
println(a) // 20
a = a + 10
println(a) // 30
}
Since applying addition or subtraction to a value is a common operation, Kotlin has special support for that. Instead of a = a + b you can use a += b.
fun main() {
var a = 10
println(a) // 10
a += 10
println(a) // 20
a += 10
println(a) // 30
}
This is called an augmented assignment, and it is supported for other operations as well, such as:
- a += b is an alternative to a = a + b,
- a -= b is an alternative to a = a — b,
- a *= b is an alternative to a = a * b,
- a /= b is an alternative to a = a / b,
- a %= b is an alternative to a = a % b.
fun main() {
var a = 10
println(a) // 10
a *= 3
println(a) // 30
a -= 12
println(a) // 18
a /= 3
println(a) // 6
a %= 4
println(a) // 2
}
Prefix and postfix operation
In Kotlin, there are some different ways to add or subtract 1.
For example
a = a+1
or
a++
and likewise for subtraction.
That a++ is called a postfix operation, which means that if you use that operation in an arithmetic equation, the postfix operation isexecuted after the equation is evaluated.
For example:
var a = 5
val eq = 6 + a++ // the result will be 11 and then a will be = 6
valnewEq = 6+a // the result will be = 12
A prefix operation works differently. If you use the prefix operation in an arithmetic equation, the prefix operation is executed before the equation is evaluated.
With the previous example a prefix operation works like this:
var a = 5
val eq = 6 + ++ a// the result a will be 6 and then eq will be = 12
valnewEq = 6+a // the result will be =12
The same follows if you use postfix and prefix operations in subtraction (a — and — a) .
fun main() {
var a = 10
println(a) // 10
a++
println(a) // 11
a--
println(a) // 10
}
Tip: a++ is an alternative to a = a + 1, a — is an alternative to a = a — 1