Instruction 3
Learning Basic Data Types: Numbers
Types are how we identify different kinds of data from the real world. Kotlin has many different basic types, including integers, floating-point numbers, booleans, characters, and strings. In this lesson, you’ll cover the number types.
Int
In Kotlin, every numeric value is automatically an Int. An Int has a maximum and minimum value of between -2,147,483,648 (-2^31^) and 2,147,483,647 (-2^31^ - 1). If a value exceeds this limit, the variable becomes a Long. A Long is also a whole number but has a higher capacity than an Int. You have already used the Int type in the previous examples. To initialize a Long, append an L to the number:
fun main() {
val amount = 100L
println(amount)
}
Note: Kotlin Integers also include
ByteandShort.
Kotlin has this neat little feature that allows you to write Long values in a readable manner. If you had a value in the hundreds of thousands or in the millions or more, you could separate every three digits with an underscore:
fun main() {
val amount = 12_000_000_000
println(amount)
}
Floating-point
Floating-point types are numbers with decimals or fractions. Single-precision or decimal numbers holding 32 bits of data, are assigned the Float class type when creating variables. Your program at this point handles the Int type only. This is enforced by toInt():
import java.util.Calendar
const val WEEKLY_INTEREST = 100
fun main(args: Array<String>) {
val mondayAmount = args[0].toInt()
val tuesdayAmount = args[1].toInt()
val wednesdayAmount = args[2].toInt()
val thursdayAmount = args[3].toInt()
val fridayAmount = args[4].toInt()
var totalWeeklyAmount = mondayAmount + tuesdayAmount + wednesdayAmount + thursdayAmount + fridayAmount
totalWeeklyAmount = totalWeeklyAmount + WEEKLY_INTEREST
val weekNumber = getWeekNumber()
println("In week number $weekNumber, you have saved $$totalWeeklyAmount.")
}
fun getWeekNumber(): Int {
val calendar: Calendar = Calendar.getInstance()
val weekOfYear = calendar.get(Calendar.WEEK_OF_YEAR)
return weekOfYear
}
toInt() means you can’t accurately record decimal amounts like $5.50. If you attempt to use a decimal amount, you’ll get an error. Change the first amount in the program arguments to 15.0, 15.0 25 12 40 250 and run the program:
Exception in thread "main" java.lang.NumberFormatException: For input string: "15.0"
at java.lang.NumberFormatException.forInputString (:-1)
at java.lang.Integer.parseInt (:-1)
at java.lang.Integer.parseInt (:-1)
Change all toInt() to toFloat() to convert the amounts you’re receiving to floats. This converts the amounts to floats so your program can handle single precision amounts:
val mondayAmount = args[0].toFloat()
val tuesdayAmount = args[1].toFloat()
val wednesdayAmount = args[2].toFloat()
val thursdayAmount = args[3].toFloat()
val fridayAmount = args[4].toFloat()
Run the code and notice that $442 is now $442.0 - a float.
In week number 3, you have saved $442.0.
Returning to our earlier code example, to explicitly initialize a floating point number, append an f to the number:
fun main() {
val amount = 100f
println(amount)
}
Double
For higher or double-precision numeric data, use the Double class type. The Double stores 64-bits of numeric data. To initialize a Double, use precision or a decimal point.
val amount = 100.0
println(amount)
Did you notice that the output of the Float, 100f and the Double, 100.0, was the same? It was 100.0. So, how do you know if 100.0 is a Float or a Double? We’ll cover that in the next lesson!
Kotlin has this neat little feature that allows you to write long figures in a readable manner. If you had a figure in the hundreds of thousands or in the millions or more, you could separate every thousand with an underscore, _:
val amount = 12_000_000_000
println(amount)