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
🌐
Baeldung
baeldung.com › home › kotlin › kotlin numbers › rounding numbers in kotlin
Rounding Numbers in Kotlin | Baeldung on Kotlin
September 7, 2024 - To sum it up, we can perform rounding in Kotlin in many ways, although the one we choose may depend on our use case. The code backing this article is available on GitHub.
🌐
GitHub
github.com › JetBrains › kotlin › blob › master › libraries › stdlib › js › src › kotlin › math.kt
kotlin/libraries/stdlib/js/src/kotlin/math.kt at master · JetBrains/kotlin
* @sample samples.math.MathSamples.Doubles.roundingModes · */ @SinceKotlin("1.2") @InlineOnly · public actual inline fun ceil(x: Double): Double = nativeMath.ceil(x) · /** * Rounds the given value [x] to an integer towards negative infinity.
Author   JetBrains
Discussions

java - Round Double to 1 decimal place kotlin: from 0.044999 to 0.1 - Stack Overflow
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 ge... More on stackoverflow.com
🌐 stackoverflow.com
How do you round a number to N decimal places - Support - Kotlin Discussions
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 More on discuss.kotlinlang.org
🌐 discuss.kotlinlang.org
2
August 2, 2018
android - how to round a number in kotlin - Stack Overflow
I used String.format() to round to the third decimal place. However this didn't work and I solved it using DecimalFormat. Is there anything I implemented wrong? val value = 36.295f Timber.e("f... More on stackoverflow.com
🌐 stackoverflow.com
Strange behavior of kotlin.math.round

Yeah, it can seem poorly named coming from Java. It is using Math.rint. If you want Math.round, you have to use roundToInt or roundToLong (except Kotlin, unlike the JVM lib, will error on NaN).

More on reddit.com
🌐 r/Kotlin
7
14
March 29, 2018
🌐
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....
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

🌐
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.
🌐
GitHub
github.com › lightningkite › kotlin-core › blob › master › src › main › java › com › lightningkite › kotlin › math › Rounding.kt
kotlin-core/src/main/java/com/lightningkite/kotlin/math/Rounding.kt at master · lightningkite/kotlin-core
August 10, 2016 - package com.lightningkite.kotlin.math · · /** * Various rounding functions · * Created by joseph on 8/10/16. */ · inline fun Float.round() = Math.round(this) ·
Author   lightningkite
🌐
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 …
Find elsewhere
🌐
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.
🌐
GitHub
github.com › romainguy › kotlin-math
GitHub - romainguy/kotlin-math: Set of Kotlin APIs to make graphics math easier to write.
Set of Kotlin APIs to make graphics math easier to write. - romainguy/kotlin-math
Starred by 879 users
Forked by 65 users
Languages   Kotlin 100.0% | Kotlin 100.0%
🌐
Medium
agrawalsuneet.medium.com › math-round-vs-math-floor-vs-math-ceil-kotlin-49bdfce5491f
Math.round vs Math.floor vs Math.ceil : Kotlin | by Suneet Agrawal | Medium
April 13, 2022 - Kotlin has a few inbuilt functions which can do the rounding up for us but they are a bit confusing. To decide when to use what, we need to understand which rounding function rounds up in which direction and which data types it can round up. This post was originally posted at https://agrawalsuneet.github.io/blogs/math...
🌐
Programiz
programiz.com › kotlin-programming › examples › round-number-decimal
Kotlin Program to Round a Number to n Decimal Places
We declare the format using the # patterns #.###. This means, we want num upto 3 decimal places. We also set the rounding mode to Ceiling, this causes the last given place to be rounded to its next number.
🌐
Codecademy
codecademy.com › docs › kotlin › math methods › floor()
Kotlin | Math Methods | floor() | Codecademy
October 14, 2023 - The following example demonstrates a basic implementation of the floor() method. ... The result will be a float value of 3 as the function rounds 3.4 down to 3. ... Learn more about how to get involved.
🌐
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.
🌐
GitHub
github.com › vittee › kformula
GitHub - vittee/kformula: Mathematical expression engine written in Kotlin, running on JVM. · GitHub
To do so, you can retreive the runtime variable with a callback function, then use it by "$external" (Kotlin) or "$external" (Java). ... final Formula fx = new Formula(); fx.addExternalVariable("$external", s -> BigDecimal.valueOf(getValue())); ...
Starred by 31 users
Forked by 3 users
Languages   Kotlin
🌐
Kotlin
kotlinlang.org › api › latest › jvm › stdlib › kotlin.math › floor.html
floor - Kotlin Programming Language
Try the revamped Kotlin docs design! ... 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.