我写了一个int 转float demo , kt代码.
fun divide(a:Int,b:Int):Float{
return a as Float / b as Float;
}
报错:Kotlin casting int to float,意思是类型转换错误
应该这么写
fun divide(a:Int,b:Int):Float{
return a.toFloat() / b.toFloat();
}
那 xx as Float 和 xx.toFloat() 有什么区别
前者属于对象类型转换
后者属于基本类型转换
本文详细解析了Kotlin中Int转Float的正确方式,对比了asFloat与toFloat()的区别,阐述了对象类型转换与基本类型转换的概念。
434

被折叠的 条评论
为什么被折叠?



