Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How multiply nullsafe float in kotlin?

Consider code example:

val contentLength :Long? = 1
val float = contentLength?.toFloat()
val any = (float ?: 0)  * 1.25
//                      ^
//           compilation error here 

If I try to extract variable herem like that:

val casted = (float ?: 0)

IDE shows that casted has Any as type. Why it happens? How to get nullsafe float value from float reference and multiply it to another float value?

UPDATED

Replacing 0 with 0.0:

(float ?: 0.0)

has no effect. :(

like image 776
Cherry Avatar asked Dec 13 '25 17:12

Cherry


1 Answers

Change the line

val any = (float ?: 0.0) * 1.25

to

val any = (float ?: 0.0f) * 1.25f

Else you mix double and float what leads to the compile error

like image 141
guenhter Avatar answered Dec 16 '25 20:12

guenhter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!