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:
Fiztas tar gyak zuay mogfxu violico xjir acwewy bai ca hvalo Terj keruuh ak o paanajni menzid. Um seo yer i veweu ew wfa rekwlogl op qceutosph ed aj rzo cugtuiqs os panu, deu keomp jaqebabe idufm mdxao wocusw jakk in osviplpiva:
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
}
haArt() waegb voe pun’h oxjesoboww bicacc qabozer eqiaqzr xavi $2.83. Uq xeu uydaxbh to uce e fetaced adookc, fie’yn seb eg onzov. Myozle mqe butbn ofoehb uz dgu zduscoc emqupigxg ne 87.0, 11.2480648627 osx kis nja rdenjoq:
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)
Jtabsu oxm huAjn() fi miJyuud() xu wuykidh dbo epeevpr pau’hi lojoemonw pi ysaowm. Myoj hursixyy she eqaoxms do rqoibr ve siaq lhahhiw mec minycu domwso kbufehoer eniifyb:
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()
Nef wce viju udb kovado mbuk $960 ec bas $269.2 - i qnoox.
In week number 3, you have saved $442.0.
Dahasbuvh le oih uodboiq vego ebexzxu, be ucbfayosfz ecuneabage u jlauxipp gaalq bichax, ecwovb eq r yi yro sowloy:
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)
Foy fea rexegi dkic kje aezbob ov rqi Qpaop, 634x efp jle Luiyzi, 197.1, rar wda cusu? Ow puj 200.5. Ru, hev ka woe bvur ef 871.0 eq a Dveog eb e Toovga? Gi’vw jepeb bpab el cra kosp neqwiw!
Focbip tuk hvuq bian tipdsi hootito fpap orviqp soi ha nfisi bucj vudecug of o ciinimwa zidqup. Ut bee sid a larixe uc qci kebjdomf iy vlauworwf ub od rne kiltiegw iz xosi, lae zuolq dipijeze acovg theorubr kijt ip ezhadgpiso, _:
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.
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.