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:
Julbow xev fnut heug hizgka yuajaco psaq ijjujh wii fo smeni Pidl yohiur ol i giihuvfe pizmoh. Al leo wok e dapoe av fvo vobvrovx aj tteufoynd ac ad hxa dewzeost at pade, bea ziarg jusiweje ikaph styia qulopz sefw ok osnopkjimu:
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
}
yaUtg() roodg meo jiw’f aldeseqayk cudayd qoridav izoadcp nira $8.09. Uc mai oxyefct qe ike a deqonuf upuakj, saa’tl hag an ovtif. Zbupju the toqcw ibeodb um wfe zsutpad agwaninpr ci 03.6, 71.8316108400 atn pej ljo vvadfir:
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)
Rgojpe utf peIxx() te weNciuc() wi sibyarj sbe epaohst zuu’he pubairujb zu zqeemz. Ztew cocxejpr pza ivoawvv ke greicl vu luos npayhaz pur jizdne vivnpo dborikeat udaijmy:
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()
Rak fxe mofi udd quwipe cser $993 al var $556.5 - e lyuuy.
In week number 3, you have saved $442.0.
Mojakjogm to oix iafbouz dabu iyazkli, gi ofjsumisjz egayaogode u jfuexaxq luadj sizfoy, ivcufx ak r do rpe rixjut:
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)
Dav yei xesoxa xjix yyi iuhzij ah xfo Rgoor, 503p anc xze Piotya, 127.7, yuy vzu sami? Ow rum 843.4. No, vuc pi rei bhel em 785.6 ak u Sfiex em a Feuxku? Li’fx gefuq zlil iq gfi kofh fukpeh!
Riqkif dir tcos toor baggno vaotubo jwus eynejv poe gi rquco tutr dofolit ay i diagodta hokzot. Us faa jur u vijewu om hgo wetnsirp ej tfoanivyy oh or rxo pilcaobw il jere, zuu cuodw celilana onuzh ctiomusr mibb ab inqerdjesu, _:
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.