圆形双倍到1位小数kotlin:从0.044999到0.1

时间:2018-02-27 15:09:48

标签: java android kotlin rounding

我有一个0.0449999的Double变量,我希望将其四舍五入到{1}}。

我正在使用Kotlin,但Java解决方案也很有帮助。

0.1

我尝试使用这两个解决方案获得1位小数:

  1. val number:Double = 0.0449999
  2. val solution = Math.round(number * 10.0) / 10.0
  3. 问题是我在两种情况下都得到0.0,因为它将数字从val solution = String.format("%.1f", number)四舍五入到0.04。它不会取所有小数并围绕它。

    我想获得0.1:0.0

7 个答案:

答案 0 :(得分:25)

最后我做了Andy Turner建议的内容,四舍五入到3位小数,然后是2再到1:

回答1:

val number:Double = 0.0449999
val number3digits:Double = String.format("%.3f", number).toDouble()
val number2digits:Double = String.format("%.2f", number3digits).toDouble()
val solution:Double = String.format("%.1f", number2digits).toDouble()

回答2:

val number:Double = 0.0449999
val number3digits:Double = Math.round(number * 1000.0) / 1000.0
val number2digits:Double = Math.round(number3digits * 100.0) / 100.0
val solution:Double = Math.round(number2digits * 10.0) / 10.0

结果

  

0.045→0.05→0.1

注意:我知道它不应该如何工作但有时你需要考虑到某些特殊情况的所有小数,所以也许有人觉得这很有用。

答案 1 :(得分:13)

BigDecimal四舍五入包含几个RoundingMode s,包括那些向上舍入(远离零)或向正无穷大舍入的值。如果这就是您所需要的,您可以按以下方式致电setScale进行四舍五入:

val number = 0.0449999
val rounded = number.toBigDecimal().setScale(1, RoundingMode.UP).toDouble()
println(rounded) // 0.1

但请注意,它的工作方式也会在0.00.10.1之间进行舍入(例如0.000010.1

.toBigDecimal()扩展程序可用since Kotlin 1.2

答案 2 :(得分:10)

始终提防语言环境!

使用未指定的语言环境,您可能会遇到偶然的问题(例如,使用葡萄牙语语言环境)

Fatal Exception: java.lang.NumberFormatException
For input string: "0,1"

1。使用DecimalFormat方法的解决方案

fun Float.roundToOneDecimalPlace(): Float {
    val df = DecimalFormat("#.#", DecimalFormatSymbols(Locale.ENGLISH)).apply {
        roundingMode = RoundingMode.HALF_UP
    }
    return df.format(this).toFloat()
}

2。使用字符串格式方法的解决方案

fun Float.roundTo(decimalPlaces: Int): Float {
    return "%.${decimalPlaces}f".format(Locale.ENGLISH,this).toFloat()
}

答案 3 :(得分:5)

我知道上述解决方案中的某些可以完美地工作,但是我想添加另一个使用ceil和floor概念的解决方案,我认为该解决方案针对所有情况进行了优化。

如果要让小数点后两位的最大值使用此值。

 -import java.math.BigDecimal 
 -import java.math.RoundingMode
 -import java.text.DecimalFormat

此处 1.45678 = 1.46

fun roundOffDecimal(number: Double): Double? {
        val df = DecimalFormat("#.##")
        df.roundingMode = RoundingMode.CEILING
        return df.format(number).toDouble()
    }

如果您希望小数点后两位的最小值,请使用此值。

此处 1.45678 = 1.45

fun roundOffDecimal(number: Double): Double? {
        val df = DecimalFormat("#.##")
        df.roundingMode = RoundingMode.FLOOR
        return df.format(number).toDouble()
    }

还有其他类似下面的标志 1.FLOOR 2.天花板 3.下 4,半场 5,万圣节前夕 6.半身 7,不必要 8.UP

详细信息在docs

中给出

答案 4 :(得分:4)

1。方法(使用Noelia的想法):

您可以在字符串模板中传递所需的小数位数,然后通过以下方式使precision变量:

fun Number.roundTo(numFractionDigits: Int) 
 = String.format("%.${numFractionDigits}f", toDouble()).toDouble()

2。方法(数字,无字符串转换)

fun roundToDecimals(number: Double, numDecimalPlaces: Int): Double {
    val factor = Math.pow(10.0, numDecimalPlaces.toDouble())
    return Math.round(number * factor) / factor
}

答案 5 :(得分:1)

试试这个, 它对我有用

     val number = 0.045
     var filterUserPrice: String? = "%.2f".format(number)
     Log.v("afterRoundoff"," : $filterUserPrice")// its print filterUserPrice is 0.05

答案 6 :(得分:0)

浮点和双精度扩展功能示例,四舍五入到小数点后n位。

    fun Float.roundTo(n : Int) : Float {
      return "%.${n}f".format(this).toFloat()
    }
    fun Double.roundTo(n : Int) : Double {
      return "%.${n}f".format(this).toDouble()
    }
相关问题