Numbers

Numbers in Kotlin can be represented with integer types and floating point number types. Integer types are ByteShortInt, and Long.

Floating point number types are Float, and Double.

The following table shows all the number types with their memory sizes and default values:

Type
Size
Default Value
Byte
8 bit
0
Short
16 bit
0
Int
32 bit
0
Long
64 bit
0L
Float
32 bit
0.0F
Double
64 bit
0.0D

 

The following example shows how to initialize number types with literals:

val long: Long = 9999L

val int: Int = 999

val short: Short = 99

val byte: Byte = 9



val float: Float = 9.99F

val double: Double = 9.99999

Kotlin also allows the use of hexadecimal and binary numbers in literals:

val hex: Int = 0XFF

val binary:Int = 0b1001001

All number types have extension functions in the Kotlin standard library for converting to other number types. Here's how you can convert an Int type to all other number types:

val int = 1
val float = int.toFloat()
val double = int.toDouble()
val long = int.toLong()
val byte = int.toByte()
val short = int.toShort()
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.206.43