简体   繁体   English

将 Double 舍入到小数点后一位 kotlin:从 0.044999 到 0.1

[英]Round Double to 1 decimal place kotlin: from 0.044999 to 0.1

I have a Double variable that is 0.0449999 and I would like to round it to 1 decimal place 0.1 .我有一个 Double 变量,它是0.0449999我想将它四舍五入到小数点后一位0.1

I am using Kotlin but the Java solution is also helpful.我正在使用 Kotlin,但 Java 解决方案也很有用。

val number:Double = 0.0449999

I tried getting 1 decimal place with these two solutions:我尝试使用这两种解决方案获得小数点后一位:

  1. val solution = Math.round(number * 10.0) / 10.0
  2. val solution = String.format("%.1f", number)

The problem is that I get 0.0 in both cases because it rounds the number from 0.04 to 0.0 .问题是我在这两种情况下都得到 0.0 因为它将数字从0.04入到0.0 It doesn't take all decimals and round it.它不需要所有小数并将其四舍五入。

I would like to obtain 0.1: 0.045 -> 0.05 -> 0.1我想获得 0.1: 0.045 -> 0.05 -> 0.1

I have a Double variable that is 0.0449999 and I would like to round it to 1 decimal place 0.1 .我有一个Double变量,它是0.0449999 ,我想将其舍入到小数点后1位0.1

I am using Kotlin but the Java solution is also helpful.我正在使用Kotlin,但Java解决方案也有帮助。

val number:Double = 0.0449999

I tried getting 1 decimal place with these two solutions:我尝试使用以下两种解决方案获得小数点后一位:

  1. val solution = Math.round(number * 10.0) / 10.0
  2. val solution = String.format("%.1f", number)

The problem is that I get 0.0 in both cases because it rounds the number from 0.04 to 0.0 .问题是在两种情况下我都得到0.0,因为它将数字从0.040.0 It doesn't take all decimals and round it.并不需要所有的小数点并四舍五入。

I would like to obtain 0.1: 0.045 -> 0.05 -> 0.1我想获得0.1: 0.045 -> 0.05 -> 0.1

I have a Double variable that is 0.0449999 and I would like to round it to 1 decimal place 0.1 .我有一个Double变量,它是0.0449999 ,我想将其舍入到小数点后1位0.1

I am using Kotlin but the Java solution is also helpful.我正在使用Kotlin,但Java解决方案也有帮助。

val number:Double = 0.0449999

I tried getting 1 decimal place with these two solutions:我尝试使用以下两种解决方案获得小数点后一位:

  1. val solution = Math.round(number * 10.0) / 10.0
  2. val solution = String.format("%.1f", number)

The problem is that I get 0.0 in both cases because it rounds the number from 0.04 to 0.0 .问题是在两种情况下我都得到0.0,因为它将数字从0.040.0 It doesn't take all decimals and round it.并不需要所有的小数点并四舍五入。

I would like to obtain 0.1: 0.045 -> 0.05 -> 0.1我想获得0.1: 0.045 -> 0.05 -> 0.1

I have a Double variable that is 0.0449999 and I would like to round it to 1 decimal place 0.1 .我有一个Double变量,它是0.0449999 ,我想将其舍入到小数点后1位0.1

I am using Kotlin but the Java solution is also helpful.我正在使用Kotlin,但Java解决方案也有帮助。

val number:Double = 0.0449999

I tried getting 1 decimal place with these two solutions:我尝试使用以下两种解决方案获得小数点后一位:

  1. val solution = Math.round(number * 10.0) / 10.0
  2. val solution = String.format("%.1f", number)

The problem is that I get 0.0 in both cases because it rounds the number from 0.04 to 0.0 .问题是在两种情况下我都得到0.0,因为它将数字从0.040.0 It doesn't take all decimals and round it.并不需要所有的小数点并四舍五入。

I would like to obtain 0.1: 0.045 -> 0.05 -> 0.1我想获得0.1: 0.045 -> 0.05 -> 0.1

I have a Double variable that is 0.0449999 and I would like to round it to 1 decimal place 0.1 .我有一个Double变量,它是0.0449999 ,我想将其舍入到小数点后1位0.1

I am using Kotlin but the Java solution is also helpful.我正在使用Kotlin,但Java解决方案也有帮助。

val number:Double = 0.0449999

I tried getting 1 decimal place with these two solutions:我尝试使用以下两种解决方案获得小数点后一位:

  1. val solution = Math.round(number * 10.0) / 10.0
  2. val solution = String.format("%.1f", number)

The problem is that I get 0.0 in both cases because it rounds the number from 0.04 to 0.0 .问题是在两种情况下我都得到0.0,因为它将数字从0.040.0 It doesn't take all decimals and round it.并不需要所有的小数点并四舍五入。

I would like to obtain 0.1: 0.045 -> 0.05 -> 0.1我想获得0.1: 0.045 -> 0.05 -> 0.1

I have a Double variable that is 0.0449999 and I would like to round it to 1 decimal place 0.1 .我有一个Double变量,它是0.0449999 ,我想将其舍入到小数点后1位0.1

I am using Kotlin but the Java solution is also helpful.我正在使用Kotlin,但Java解决方案也有帮助。

val number:Double = 0.0449999

I tried getting 1 decimal place with these two solutions:我尝试使用以下两种解决方案获得小数点后一位:

  1. val solution = Math.round(number * 10.0) / 10.0
  2. val solution = String.format("%.1f", number)

The problem is that I get 0.0 in both cases because it rounds the number from 0.04 to 0.0 .问题是在两种情况下我都得到0.0,因为它将数字从0.040.0 It doesn't take all decimals and round it.并不需要所有的小数点并四舍五入。

I would like to obtain 0.1: 0.045 -> 0.05 -> 0.1我想获得0.1: 0.045 -> 0.05 -> 0.1

I have a Double variable that is 0.0449999 and I would like to round it to 1 decimal place 0.1 .我有一个Double变量,它是0.0449999 ,我想将其舍入到小数点后1位0.1

I am using Kotlin but the Java solution is also helpful.我正在使用Kotlin,但Java解决方案也有帮助。

val number:Double = 0.0449999

I tried getting 1 decimal place with these two solutions:我尝试使用以下两种解决方案获得小数点后一位:

  1. val solution = Math.round(number * 10.0) / 10.0
  2. val solution = String.format("%.1f", number)

The problem is that I get 0.0 in both cases because it rounds the number from 0.04 to 0.0 .问题是在两种情况下我都得到0.0,因为它将数字从0.040.0 It doesn't take all decimals and round it.并不需要所有的小数点并四舍五入。

I would like to obtain 0.1: 0.045 -> 0.05 -> 0.1我想获得0.1: 0.045 -> 0.05 -> 0.1

For new comers对于新来者

Using String.format for decimal precision can lead to problems for different languages.将 String.format 用于小数精度可能会导致不同语言出现问题。

Use the following code to convert Double to as many decimal places as you want.使用以下代码将 Double 转换为所需的小数位数。

val price = 6.675668

//to convert to 2 decimal places
totalTime = Math.round(totalTime * 100.0) / 100.00
// totalTime = 6.68

//to convert to 4 decimal places
totalTime = Math.round(totalTime * 10000.0) / 10000.00
// totalTime = 6.6757

I have a Double variable that is 0.0449999 and I would like to round it to 1 decimal place 0.1 .我有一个Double变量,它是0.0449999 ,我想将其舍入到小数点后1位0.1

I am using Kotlin but the Java solution is also helpful.我正在使用Kotlin,但Java解决方案也有帮助。

val number:Double = 0.0449999

I tried getting 1 decimal place with these two solutions:我尝试使用以下两种解决方案获得小数点后一位:

  1. val solution = Math.round(number * 10.0) / 10.0
  2. val solution = String.format("%.1f", number)

The problem is that I get 0.0 in both cases because it rounds the number from 0.04 to 0.0 .问题是在两种情况下我都得到0.0,因为它将数字从0.040.0 It doesn't take all decimals and round it.并不需要所有的小数点并四舍五入。

I would like to obtain 0.1: 0.045 -> 0.05 -> 0.1我想获得0.1: 0.045 -> 0.05 -> 0.1

Try this way for two decimal value return as string尝试这种方式将两个十进制值作为字符串返回

private fun getValue(doubleValue: Double): String {
    return String.format(Locale.US, "%.2f", doubleValue)
}

In Kotlin I just use this function:在 Kotlin 中,我只使用这个函数:

fun bytesToMBFormatWithoutLetters(bytes: Long): String {

        val fileSizeInKBytes = (bytes / 1024).toFloat()
        val fileSizeInMBytes = (fileSizeInKBytes / 1024)
        return "%.2f".format(fileSizeInMBytes)

    }

you can just convert it to string and then use trim method您可以将其转换为字符串,然后使用修剪方法

3.1415926535.toString().take(5)
//OutPut =>  3.141

Use this extension function:使用此分机 function:

toBigDecimal(MathContext(3, RoundingMode.HALF_EVEN)).toPlainString()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM