Finally I did what Andy Turner suggested, rounded to 3 decimals, then to 2 and then to 1:

Answer 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()

Answer 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

Result:

0.045 → 0.05 → 0.1

Note: I know it is not how it should work but sometimes you need to round up taking into account all decimals for some special cases so maybe someone finds this useful.

Answer from Noelia on Stack Overflow
🌐
Kotlin
kotlinlang.org › api › core › kotlin-stdlib › kotlin.math › round.html
round | Core API – Kotlin Programming Language
kotlin-stdlib/kotlin.math/round · CommonJSJVMNativeWasm-JSWasm-WASI · expect fun round(x: Double): Double(source) Rounds the given value x towards the closest integer with ties rounded towards even integer.
Top answer
1 of 15
174

Finally I did what Andy Turner suggested, rounded to 3 decimals, then to 2 and then to 1:

Answer 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()

Answer 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

Result:

0.045 → 0.05 → 0.1

Note: I know it is not how it should work but sometimes you need to round up taking into account all decimals for some special cases so maybe someone finds this useful.

2 of 15
84

I know some of the above solutions work perfectly but I want to add another solution that uses ceil and floor concept, which I think is optimized for all the cases.

If you want the highest value of the 2 digits after decimal use below code.

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

here, 1.45678 = 1.46

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

If you want the lowest value of the 2 digits after decimal use below code.

here, 1.45678 = 1.45

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

Here a list of all available flags: CEILING, DOWN, FLOOR, HALF_DOWN, HALF_EVEN, HALF_UP, UNNECESSARY, UP

The detailed information is given in docs

🌐
Codecademy
codecademy.com › docs › kotlin › math methods › round()
Kotlin | Math Methods | round() | Codecademy
October 27, 2023 - The round() function in Kotlin’s math package rounds a given number to the nearest integer.
🌐
Kotlin Discussions
discuss.kotlinlang.org › support
How do you round a number to N decimal places - Support - Kotlin Discussions
August 2, 2018 - So I saw this post about my problem(Print floats with certain amount of decimal numbers) And I was wondering I it possible to use a methood or something else rather “%.2f”.format(value) in order to achive the same thing …
🌐
Kotlin
kotlinlang.org › api › latest › jvm › stdlib › kotlin.math › round.html
round
Try the revamped Kotlin docs design! ... Mathematical functions and constants. The functions include trigonometric, hyperbolic, exponentiation and power, logarithmic, rounding, sign and absolute value.
🌐
Kotlin
kotlinlang.org › api › latest › jvm › stdlib › kotlin.math › round-to-int.html
Kotlinlang
Try the revamped Kotlin docs design! ... Mathematical functions and constants. The functions include trigonometric, hyperbolic, exponentiation and power, logarithmic, rounding, sign and absolute value.
🌐
Kotlin
kotlinlang.org › api › core › kotlin-stdlib › kotlin.math › round-to-int.html
roundToInt | Core API – Kotlin Programming Language
kotlin-stdlib/kotlin.math/roundToInt · CommonJSJVMNativeWasm-JSWasm-WASI · expect fun Double.roundToInt(): Int(source) Rounds this Double value to the nearest integer and converts the result to Int.
Find elsewhere
🌐
W3cubDocs
docs.w3cub.com › kotlin › api › latest › jvm › stdlib › kotlin.math › round
kotlin.math.round - Kotlin - W3cubDocs
kotlin-stdlib / kotlin.math / round · Platform and version requirements: JVM (1.2), JS (1.2), Native (1.2) fun round(x: Double): Double · fun round(x: Float): Float · Rounds the given value x towards the closest integer with ties rounded towards even integer.
🌐
Kotlin
kotlinlang.org › api › core › kotlin-stdlib › kotlin.math › floor.html
floor | Core API – Kotlin Programming Language
Rounds the given value x to an integer towards negative infinity. ... the largest double value that is smaller than or equal to the given value x and is a mathematical integer. import kotlin.math.* import kotlin.test.* fun main() { //sampleStart ...
🌐
Runebook
runebook.dev › english › articles › kotlin
Mastering Kotlin's kotlin.math.round(): Tips & Best Practices
Return Type The function returns a Long value, which represents the rounded whole number.The function returns a Long value
🌐
Kotlin
kotlinlang.org › api › latest › jvm › stdlib › kotlin.math › floor.html
floor - Kotlin Programming Language
Rounds the given value x to an integer towards negative infinity. ... the largest Float value that is smaller than or equal to the given value x and is a mathematical integer.
🌐
Kotlin
kotlinlang.org › api › core › kotlin-stdlib › kotlin.math › round-to-long.html
roundToLong | Core API – Kotlin Programming Language
kotlin-stdlib/kotlin.math/roundToLong · CommonJSJVMNativeWasm-JSWasm-WASI · expect fun Double.roundToLong(): Long(source) Rounds this Double value to the nearest integer and converts the result to Long.
🌐
Kotlin
kotlinlang.org › api › core › kotlin-stdlib › kotlin.math › ceil.html
ceil | Core API – Kotlin Programming Language
kotlin-stdlib/kotlin.math/ceil · CommonJSJVMNativeWasm-JSWasm-WASI · expect fun ceil(x: Double): Double(source) Rounds the given value x to an integer towards positive infinity.
🌐
Kotlin
kotlinlang.org › api › latest › jvm › stdlib › kotlin.math › ceil.html
ceil - Kotlin Programming Language
August 10, 2022 - Try the revamped Kotlin docs design! ... Mathematical functions and constants. The functions include trigonometric, hyperbolic, exponentiation and power, logarithmic, rounding, sign and absolute value.
🌐
Kotlin
kotlinlang.org › api › latest › jvm › stdlib › kotlin.math › round-to-long.html
roundToLong - Kotlin Programming Language
August 2, 2018 - kotlin-stdlib / kotlin.math / ... source) (JVM source) (JS source) (Native source) Rounds this Double value to the nearest integer and converts the result to Long....
🌐
Suneet Agrawal
agrawalsuneet.github.io › blogs › math-round-vs-math-floor-vs-math-ceil-kotlin
Math.round vs Math.floor vs Math.ceil : Kotlin · Suneet Agrawal
April 11, 2022 - To decide when to use what, we ... that -3 is bigger than -4 and -3.5 is bigger than -3.51 · Math.round rounds up to the nearest Integer which can be above or below or even equal to the actual value....