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 - The BigDecimal class offers an ... RoundingMode.UP).toDouble() assertTrue(roundedUp == 0.4) With setScale(), we specify the number of decimal places to which the Double must be rounded....
🌐
Kotlinlang
slack-chats.kotlinlang.org › t › 474887 › what-is-the-most-efficient-way-to-round-double-up-to-certain
what is the most efficient way to round `Double` up to certa kotlinlang #getting-started
September 17, 2021 - val decimalPrecision = 2 val doubleFormat = "%.0${decimalPrecision}f" println(doubleFormat.format(1.4999)) The question is whether you need the rounded value or just a formatted representation of it
Discussions

java - Round Double to 1 decimal place kotlin: from 0.044999 to 0.1 - Stack Overflow
I have a Double variable that is ... decimal place 0.1 . I am using Kotlin but the Java solution is also helpful. ... 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. ... Why would you expect 0.0449999 to round to 0.1? That is mathematically incorrect. ... Well, that's a pretty meaningless way to round. But, if that's how you want to do it, do it like that: Round to 3 digits, then 2 digits, then ... More on stackoverflow.com
🌐 stackoverflow.com
Problem with rounding a number.
To my eyes, it is fundamentally wrong to try to "round an FP number to two decimals". It should only be a way of formatting a string that describes it. Most two-decimal numbers aren't exactly representable in FP so you shouldn't do any calculations after "rounding". That's why, as a matter of coding practice, the advice to use string formatting functions is correct. More on reddit.com
🌐 r/Kotlin
11
4
August 8, 2018
Print floats with certain amount of decimal numbers - Support - Kotlin Discussions
Hello, I am trying to print a float with two decimal symbols. How to do that? More on discuss.kotlinlang.org
🌐 discuss.kotlinlang.org
2
June 20, 2016
Format a double to fixed decimal length - Support - Kotlin Discussions
Hi I want to round a Double (12345.12345) to a String with a fixed length after the ‘.’ Example: 12345.123 And: if it is 12345.1 It still should give me 12345.100 How can I do this in kotlin? More on discuss.kotlinlang.org
🌐 discuss.kotlinlang.org
1
November 28, 2020
🌐
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 …
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

🌐
Luasoftware
code.luasoftware.com › tutorials › kotlin › kotlin-round-double-to-2-decimal-point
Kotlin Round Double to 2 Decimal Point - Lua Software Code
kotlin · val value = 3.14159265358979323// round to 2 decimal: 3.14"%.2f".format(value).toDouble()// orMath.round(value * 100) / 100.0 · ❤️ Is this article helpful? Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free. Do send some 💖 to @d_luaz or share ...
🌐
Programiz
programiz.com › kotlin-programming › examples › round-number-decimal
Kotlin Program to Round a Number to n Decimal Places
In the above program, we've used DecimalFormat class to round a given number num. We declare the format using the # patterns #.###. This means, we want num upto 3 decimal places.
🌐
Android Developers
developer.android.com › api reference › decimalformat
DecimalFormat | API reference | Android Developers
Skip to main content · English · Deutsch · Español – América Latina · Français · Indonesia · Polski · Português Brasil · Tiếng Việt · 中文 – 简体
Find elsewhere
🌐
Medium
medium.com › @elizarov › floating-point-for-decimals-fc2861898455
Floating-point for decimals. There is a common misconception that… | by Roman Elizarov | Medium
November 23, 2017 - We need to add our numbers so that 1.01 + 1.02 == 2.03, while a regular addition of those two doubles produces the result of 2.0300000000000002. How? We just need to round them to two decimal digits after the decimal point (we are using Kotlin 1.2 math functions here):
🌐
Kotlinlang
slack-chats.kotlinlang.org › t › 504414 › how-to-round-double-input-to-two-decimal-places-given-the-ou
How to round double input to two decimal places Given the ou kotlinlang #mathematics
How to round double input to two decimal places? Given the output is Decimal something like · toFixed(2) in JS · i · Ilya Muradyan10/28/2021, 12:03 PM · As @Iaroslav Postovalov already mentioned, this will work for you · Copy code · private val MY_FORMAT = DecimalFormat("0.##") val x = 4.378 println(MY_FORMAT.format(x)) // 4.38 ·
🌐
CopyProgramming
copyprogramming.com › howto › kotlin-kotlin-round-float-to-2-decimal-places
Kotlin: Rounding Floats to 2 Decimal Places in Kotlin
May 5, 2023 - Here is an example: val value = 106.div(20.00) val finalValue = Math.ceil(value).toInt() Format number using decimal format in kotlin, This is the format you need: val dec = DecimalFormat("#,###.##"). will print: 5.384,45. if you need always exactly 2 digits after the ...
🌐
Codecademy
codecademy.com › docs › kotlin › math methods › round()
Kotlin | Math Methods | round() | Codecademy
October 27, 2023 - Since 6 is even, the round() function returns 6.0 for both 5.5 and 6.5. ... Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more! ... Learn Kotlin, the expressive, open-source programming language developed by JetBrains.
🌐
CopyProgramming
copyprogramming.com › howto › kotlin-kotlin-format-double-to-2-decimal-places
How to Format Double to 2 Decimal Places in Kotlin: Complete Guide with 2026 Best Practices
December 20, 2025 - For display purposes, formatting is usually sufficient; for calculations involving financial data, BigDecimal is necessary to maintain precision throughout the operation chain. The simplest way to format a Double to 2 decimal places in Kotlin is using String.format(), which wraps Java's ...
🌐
Kotlin Discussions
discuss.kotlinlang.org › support
How do you round a number to N decimal places - #2 by fvasco - Support - Kotlin Discussions
August 2, 2018 - You should explain better you problem. Technically you cannot round the IEEE 754 floating point 0.1 to the first decimal point. However you can use fun Double.round(decimals: Int): Double { var multiplier = 1.0 repeat(decimals) { multiplier *= 10 } return round(this * multiplier) / multiplier }
🌐
Kotlin Discussions
discuss.kotlinlang.org › support
How do you round a number to N decimal places - #4 by jaeyeolshin - Support - Kotlin Discussions
January 16, 2019 - If you want to round a double value ... val decimal = BigDecimal(3.14159265359).setScale(2, RoundingMode.HALF_EVEN) println(decimal) // 3.14 Or if you just want to compare it with the rounded value, you can use round method. import kotlin.math.round println(round(3.14159265359 * 100) / 100 == 3.14) // true You ...
🌐
Kotlin
kotlinlang.org › api › core › kotlin-stdlib › kotlin.math › round.html
round | Core API – Kotlin Programming Language
import kotlin.math.* import kotlin.test.* fun main() { //sampleStart println(round(3.49f)) // 3.0 println(round(3.51f)) // 4.0 // 3.5 is between 3.0 and 4.0, so it is rounded towards an even number 4.0 println(round(3.5f)) // 4.0 // 2.5 is between 2.0 and 3.0, so it is rounded towards an even number 2.0 println(round(2.5f)) // 2.0 // -10.0 is already an "integer", so no rounding will take place println(truncate(-10.0f)) // -10.0 // Special cases println(round(Float.NaN)) // NaN println(round(Float.POSITIVE_INFINITY)) // Infinity println(round(Float.NEGATIVE_INFINITY)) // -Infinity //sampleEnd }
🌐
Kotlin Discussions
discuss.kotlinlang.org › support
Print floats with certain amount of decimal numbers - Support - Kotlin Discussions
June 20, 2016 - Hello, I am trying to print a float with two decimal symbols. How to do that?
🌐
Sling Academy
slingacademy.com › article › how-to-round-numbers-in-kotlin
How to Round Numbers in Kotlin - Sling Academy
If you need to round numbers to a specific number of decimal places, Kotlin allows you to use String.format() function. fun main() { val number: Double = 4.765 val roundedToTwoDecimal = "%.2f".format(number) println("Original Number: $number") println("Rounded to 2 decimal places: ...
🌐
Kotlin Discussions
discuss.kotlinlang.org › support
Format a double to fixed decimal length - Support - Kotlin Discussions
November 28, 2020 - Hi I want to round a Double (12345.12345) to a String with a fixed length after the ‘.’ Example: 12345.123 And: if it is 12345.1 It still should give me 12345.100 How can I do this in kotlin?