casting - kotlin中如何乘以nullsafe float ?

标签 casting floating-point numbers kotlin

考虑代码示例:

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

如果我尝试像这样提取变量herem:

val casted = (float ?: 0)

IDE 显示 casted 的类型为 Any。为什么会发生这种情况?如何从浮点引用获取空安全浮点值并将其乘以另一个浮点值?

已更新

0 替换为 0.0:

(float ?: 0.0)

没有效果。 :(

最佳答案

换行

val any = (float ?: 0.0) * 1.25

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

否则你混合使用doublefloat会导致编译错误

关于casting - kotlin中如何乘以nullsafe float ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46447341/

相关文章:

c - 我该如何打印这个?

SQL从int转换为十进制

c++ - C语言编程——寻找素数

java - 将以单词形式书写的数字转换为整数?

c++ - 为什么虚函数会破坏我的转换?

java - 如果我有一组独特的 Longs,如何获得一组独特的整数?

floating-point - 标准化 float f之后(之前)的下一个标准化 float 是什么?

c - getfloat 返回 23.7999

c - float 和 double 精度位数的差异

Python 3.4 : Trying to . rjust 一个完整的数字循环