0
0
Kotlinprogramming~5 mins

Int, Long, Float, Double number types in Kotlin

Choose your learning style9 modes available
Introduction

We use different number types to store numbers in the best way for our program. Each type holds numbers of different sizes and with or without decimals.

When you need to store whole numbers that fit in a small range, use Int.
When you need to store very big whole numbers, use Long.
When you need to store numbers with decimals and less precision, use Float.
When you need to store numbers with decimals and more precision, use Double.
When you want to save memory by choosing the smallest type that fits your number.
Syntax
Kotlin
val myInt: Int = 123
val myLong: Long = 1234567890123L
val myFloat: Float = 12.34F
val myDouble: Double = 12.3456789

Use L suffix for Long numbers to tell Kotlin it is a Long.

Use F suffix for Float numbers to tell Kotlin it is a Float.

Examples
This stores a whole number without decimals in an Int variable.
Kotlin
val age: Int = 30
This stores a very large whole number using Long. The L is needed.
Kotlin
val distance: Long = 9876543210L
This stores a decimal number with less precision using Float. The F is needed.
Kotlin
val price: Float = 19.99F
This stores a decimal number with more precision using Double.
Kotlin
val pi: Double = 3.1415926535
Sample Program

This program shows how to declare and print Int, Long, Float, and Double variables.

Kotlin
fun main() {
    val myInt: Int = 100
    val myLong: Long = 10000000000L
    val myFloat: Float = 10.5F
    val myDouble: Double = 10.123456789

    println("Int value: $myInt")
    println("Long value: $myLong")
    println("Float value: $myFloat")
    println("Double value: $myDouble")
}
OutputSuccess
Important Notes

Int and Long store whole numbers only, no decimals.

Float and Double store decimal numbers, but Double is more precise.

Always use suffix L for Long and F for Float to avoid errors.

Summary

Int is for normal whole numbers.

Long is for very big whole numbers.

Float is for decimal numbers with less precision.

Double is for decimal numbers with more precision.