考虑:
val foo: Int = 1
foo.toDouble() // ok
val bar = 2.toUInt()
bar.toDouble() // error!
Run Code Online (Sandbox Code Playgroud)
这对我来说没有意义。UInt为什么没有toDouble?(它也没有.toFloat)。
文档说:
每种数字类型都支持以下转换:
- toByte():字节
- toShort():短
- toInt():Int
- toLong():长
- toFloat():浮动
- toDouble():双精度
- toChar():字符
因此应该有可能。我得到的错误是:
Error:(11, 4) Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
@InlineOnly public inline fun String.toDouble(): Double defined in kotlin.text
Run Code Online (Sandbox Code Playgroud)
UInt是否不视为数字类型?或者是别的什么?
And*_*ilo 17
UInt是否不视为数字类型?
是的,它不会扩展Number类。
声明Int:
class Int : Number, Comparable<Int>
Run Code Online (Sandbox Code Playgroud)
声明UInt:
inline class UInt : Comparable<UInt>
Run Code Online (Sandbox Code Playgroud)
从Kotlin版本1.3.30开始UInt具有toFloat和toDouble方法。
| 归档时间: |
|
| 查看次数: |
1262 次 |
| 最近记录: |