我试图找到一种简单的方法来将一个双精度数转换为两位小数.我使用BigDecimal来做这个技巧,但注意到
java.math.BigDecimal类的函数
doubleValue不存在.
以下功能:
fun Double.roundTo2DecimalPlaces() =
BigDecimal(this).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue()
给我这个编译错误:
:compileKotlin
Using kotlin incremental compilation
w: The '-d' option with a directory destination is ignored because '-module' is specified
e: -{FilePathAndNameWereHere}-: (20,14): Unresolved reference: doubleValue
:compileKotlin Failed
Kotlin版本是1.1.1
解决方法
你不能使用todouble()代替,即:
fun Double.roundTo2DecimalPlaces() =
BigDecimal(this).setScale(2,BigDecimal.ROUND_HALF_UP).todouble()