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
April 28, 2026 - 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....
🌐
Kotlin Discussions
discuss.kotlinlang.org › support › how do you round a number to n decimal places
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 …
🌐
CodingTechRoom
codingtechroom.com › question › round-double-one-decimal-place-kotlin-java
How to Round a Double to One Decimal Place in Kotlin and Java - CodingTechRoom
Convert the Double to a BigDecimal, set the scale to one decimal place, and use rounding mode to achieve the correct result. For a useful code-quality reference, Clean Code is a useful companion. Mistake: Using Math.round() and expecting correct decimal output.
🌐
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 - fun Double.round(decimalPrecision: Int): Double { return BigDecimal(this.toString()) .setScale(decimalPrecision, RoundingMode.HALF_UP) .toDouble() } but it's still giving me ... 1.4999 Note: I have used that BigDecimal constructor because of this SO answer and I'm infact handling currency
🌐
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.
🌐
Kotlin Discussions
discuss.kotlinlang.org › support › format a double to fixed decimal length
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?
🌐
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.
Find elsewhere
🌐
Programiz
programiz.com › kotlin-programming › examples › round-number-decimal
Kotlin Program to Round a Number to n Decimal Places
Online Python Online JavaScript Online SQL Online Java Online HTML Online C Online C++ Online C# Online PHP Online Swift Online Kotlin Online TypeScript Online Go Online Rust Online Scala Online Dart Online R Online Ruby ... In the above program, we've used format() method to print the given floating point number num to 4 decimal places. The 4 decimal places is given by the format .4f. This means, print only upto 4 places after the dot (decimal places), and f means to print the floating point number. import java.math.RoundingMode import java.text.DecimalFormat fun main(args: Array<String>) { val num = 1.34567 val df = DecimalFormat("#.###") df.roundingMode = RoundingMode.CEILING println(df.format(num)) }
🌐
Kotlin Discussions
discuss.kotlinlang.org › support
How do you round a number to N decimal places - #16 by tagaev - Support - Kotlin Discussions
December 17, 2020 - Take a look for this method, in my case it works: fun Double.roundTo(numFractionDigits: Int): Double { val factor = 10.0.pow(numFractionDigits.toDouble()) return (this * factor).roundToInt() / factor } i borrow this from: java - Round Double to 1 decimal place kotlin: from 0.044999 to 0.1 - Stack Overflow
🌐
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 - Technically you cannot round the ... use fun Double.round(decimals: Int): Double { var multiplier = 1.0 repeat(decimals) { multiplier *= 10 } return round(this * multiplier) / multiplier }...
🌐
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 · 中文 – 简体
🌐
Medium
elizarov.medium.com › floating-point-for-decimals-fc2861898455
Floating-point for decimals. There is a common misconception that… | by Roman Elizarov | Medium
October 23, 2017 - The obvious, and much publicized solution to this problem, is to use decimal types to represent decimal numbers. In Kotlin/JVM you can use BigDecimal (.toBigDecimal extension is available in Kotlin 1.2):
Top answer
1 of 4
5

Double.toString() already does most of this for you - it just retains as many digits as necessary to distinguish the number from other doubles, so it'll strip the trailing zeroes.

The only issue is wanting 1.0 represented as 1, since that's technically an integer representation, not a double. You can write a function to take care of that case though:

// or a val with a get() to make the string representation a property
fun Double.roundToString() = when {
    toInt().toDouble() == this -> toInt()
    else -> this
}.toString()

Just FYI, if the number's small enough (less than 10¯³) toString() will produce scientific notation instead of listing all the digits with leading zeroes. If you want to handle those, you'll probably have to work with format strings, decide how much precision you want, etc.

Here's a way you can do that, with up to 10 digits of precision, and any trailing zeroes removed (and the decimal point itself, if it ends up at the end):

fun Double.roundToString() = "%.10f".format(this).trimEnd('0').trimEnd('.')

or using a regex if you're into that

fun Double.roundToString() = "%.10f".format(this).replace(Regex("\\.*0+$"), "")
2 of 4
2

I would use a simple extension function for Double, maybe like this:

fun Double.printMinimalRepresentation() {
    // check if the decimal places equal zero
    if (this % 1 > 0.0) {
        // if not, just print the number
        println(this)
    } else {
        // otherwise convert to Int and print
        println(this.toInt())
    }
}

Example values in a main function:

fun main() {
    var a: Double = 1.23
    var b: Double = 0.330
    var c: Double = 1.00
    var d: Double = 1.001

    a.printMinimalRepresentation()
    b.printMinimalRepresentation()
    c.printMinimalRepresentation()
    d.printMinimalRepresentation()
}

This prints

1.23
0.33
1
1.001

Please note that the conversion toInt() not only removes the unwanted zero but the obviously unwantet dot as well.

🌐
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 to specific decimal places and print it, you can use java.math.BigDecimal. import java.math.BigDecimal import java.math.RoundingMode 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 can do same thing with @fvasco’s extension method.
🌐
Techie Delight
techiedelight.com › home › java › round up a float or a double with 2 decimal places in kotlin
Round up a float or a double with 2 decimal places in Kotlin | Techie Delight
July 7, 2026 - This article explores different ways to round up a float or a double with 2 decimal places in Kotlin... The roundToInt() function rounds a double value to the nearest integer. It can be used as follows to round up a float or double with the ...
🌐
GitHub
gist.github.com › bastman › bad0cb8546be913f9d62f246ef6c0514
round bigdecimal, double in kotlin · GitHub
round bigdecimal, double in kotlin · Raw · RoundKt.kt · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
🌐
MojoAuth
mojoauth.com › dev guides › kotlin bigdecimal: precise decimal math
Kotlin BigDecimal: Precise Decimal Math | MojoAuth Dev Guides
September 28, 2026 - 0.1.toBigDecimal() goes through Double.toString(), so it gives 0.1. The BigDecimal(0.1) constructor instead captures the exact binary value of the double, with all its error. Prefer strings; never use the BigDecimal(Double) constructor for money. Divide BigDecimal values with an explicit scale and rounding mode. Kotlin’s / operator calls divide(other, RoundingMode.HALF_EVEN), which keeps the scale of the left operand, so BigDecimal("10") / BigDecimal("3") returns 3, not 3.33.
🌐
Kotlinlang
slack-chats.kotlinlang.org › t › 10076447 › how-do-i-round-a-double-to-certain-decimals-in-kmm-for-examp
How do I round a double to certain decimals in KMM For examp kotlinlang #multiplatform
March 21, 2023 - internal fun Double.fractionDigits(fractionDigits: Int): Double { val factor = 10.0.pow(fractionDigits) return round(factor * this) / factor } ... Will there be issues with double calculation inaccuracy? For example, I don't want 2.64.fractionDigits(1) to be like 2.60000000000001