我需要使用 BigInteger,但在 kotlin 中找不到类似的东西。
kotlin 中是否有替代 java 的 BigInteger 的类?
或
我应该将 java 类导入 kotlin 吗?
最佳答案
java.math.BigInteger
可以在 Kotlin 中像任何其他 Java 类一样使用。甚至还有helpers in stdlib使普通操作更容易读写。您甚至可以自己扩展帮助程序以提高可读性:
import java.math.BigInteger
fun Long.toBigInteger() = BigInteger.valueOf(this)
fun Int.toBigInteger() = BigInteger.valueOf(toLong())
val a = BigInteger("1")
val b = 12.toBigInteger()
val c = 2L.toBigInteger()
fun main(argv:Array<String>){
println((a + b)/c) // prints out 6
}
关于kotlin - Kotlin 中的大整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44287466/