Kotlin Float保留小数点后二位

一. 直接使用"%.2f".format(folat) 会自动执行四舍五入

二. 使用DecimalFormat

fun getFloatNoMoreThanTwoDigits(number: Float): String {
    val format = DecimalFormat("#.##")
    //舍弃规则,RoundingMode.FLOOR表示直接舍弃。
    format.roundingMode = RoundingMode.FLOOR
    return format.format(number)
}

你可能感兴趣的:(kotlin,kotlin,开发语言,android)