简体   繁体   中英

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 .

I am using Kotlin but the Java solution is also helpful.

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 . It doesn't take all decimals and round it.

I would like to obtain 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 .

I am using Kotlin but the Java solution is also helpful.

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 . It doesn't take all decimals and round it.

I would like to obtain 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 .

I am using Kotlin but the Java solution is also helpful.

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 . It doesn't take all decimals and round it.

I would like to obtain 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 .

I am using Kotlin but the Java solution is also helpful.

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 . It doesn't take all decimals and round it.

I would like to obtain 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 .

I am using Kotlin but the Java solution is also helpful.

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 . It doesn't take all decimals and round it.

I would like to obtain 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 .

I am using Kotlin but the Java solution is also helpful.

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 . It doesn't take all decimals and round it.

I would like to obtain 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 .

I am using Kotlin but the Java solution is also helpful.

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 . It doesn't take all decimals and round it.

I would like to obtain 0.1: 0.045 -> 0.05 -> 0.1

For new comers

Using String.format for decimal precision can lead to problems for different languages.

Use the following code to convert Double to as many decimal places as you want.

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 .

I am using Kotlin but the Java solution is also helpful.

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 . It doesn't take all decimals and round it.

I would like to obtain 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:

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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