You can not make a cast
directly a class Double
to a class Int
. Also, Double
e Int
are immutable objects, so you can not modify them in any way.
Primarily, if the cast can be done, but in this case classes are being handled, so if they do not have any inheritance relationship, it is impossible to make the cast
because it will cause an exception.
In your example you have the following:
var variable1=12.11 as Int
The most you can do that way is to avoid the exception and return null with as?
instead of as
in the conversion.
According to the Kotlin official documentation the values marked as Double you can get the whole of it with the function toInt()
so in your case you can do the following:
var variable1:Int = 12.11.toInt()
All types or numerical representation classes support the following methods:
toByte(): Byte
toShort(): Short
toInt(): Int
toLong(): Long
toFloat(): Float
toDouble(): Double
toChar(): Char
You can see more examples in the Documentation on the basic types .