Converting from Double to Integer

Similarly, we can convert a Double variable into an Integer. Use the toInt() function to convert from Double to Int as follows:

fun main (args: Array<String>) {
var doubleValue : Double = 12.345
var intValue = doubleValue.toInt()
println("From Double $doubleValue to Int $intValue")
}

When converting from one type to another, we must take data loss into consideration. The following is the output of this example:

From Double 12.345 to Int 12

Double belongs to the real data type family. It can store values containing a decimal point. In contrast, Integer belongs to the number data type, which deals with whole numbers only. When the Double data type is converted to an Integer, the intValue variable simply ignores the .345 fraction value.

..................Content has been hidden....................

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