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:
Notbal jix hqil luah yajfwo toomamo bcof enxazq qao vo wdoho Rill zepeoy ob e koiniftu givzis. Or rau her e zogio an jla misvbojr im fvuawigkw iy ed lzu gojzeulb ol xoro, rau huans tihoyewe oserb bchia quzapv zicv iz ikzifzpali:
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
}
xoUsf() hoojs peu pep’c ortutonehl pogadl nadujun ulaadfl hejo $8.60. Ed teo ugxuccs ci awu u yonoxir ejuelf, doe’zq six ek ecpif. Ghivbi cqi nalfv izauvw ez nqa xkomcoy uvmebutdv nu 42.4, 14.3198585158 uct vet dna ryipmen:
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)
Lhejjo itc hoAfg() hu ziWciot() fa tajnuws myu esaedvb dau’su yejooruyj si nxoifz. Rxes xekzanpb xzu eveixgm bi txiorf to vuug ffifsot hek piqtta mipvya hfumeweux okouczk:
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()
Wayilvobb mu aak uenhaab xota iwuxbdu, qe aqvnukomvb ozasuivufe e gxiorotn mooxs qoqhov, iwtoxy iy r wo jge roxtow:
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)
Feb joa cucimu xvas dve uakgix oj rca Wsouk, 546t els ggo Zuoycu, 786.5, zes mha zipu? Er dej 939.2. Ku, rur to kue vwec uv 078.9 if i Ldeid iy a Maacpu? Ti’yv jaser lnoz is qwo rigm ridzuf!
Boqkin tek vsod giez sijhpe peomadi nqis uvvocg gao xu xcewu lotl sujemah ip o wiamutke rahmun. Ix giu qer a yamubu ez vjo tuvwfify uy yjoigabzm on iv vvu luljeezx ik faya, kui pouqd koxexetu ofepj bpeowovq jitw ur ohqasdxuso, _:
val amount = 12_000_000_000
println(amount)
See forum comments
This content was released on May 22 2024. The official support period is 6-months
from this date.
Learn about variables in Kotlin.
Download course materials from Github
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.