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
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 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 …
Discussions

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
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
Converting Double to String with specific number of decimals
Have you considered String.format("%.10f", result)? More on reddit.com
🌐 r/Kotlin
2
4
May 29, 2021
DecimalFormat for Kotlin Multiplatform
I just used the expect/actual modifiers to create a formatting interface in the Multiplatform library, and then implemented it on Android using DecimalFormat and on iOS with NSNumberFormatter. It's a little extra work, but not too bad. More on reddit.com
🌐 r/Kotlin
4
2
September 21, 2020
🌐
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 - Double up to certain decimal points, there is · round() function in Kotlin but it always completely rounds up to 0 decimals? I want something like · round(1.4999,decimalPrecision = 2) // 1.50 · j · Joffrey09/17/2021, 2:11 PM · If your goal ...
🌐
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 ...
🌐
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 ·
🌐
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.
🌐
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 - This guide explores multiple proven methods to format doubles with 2 decimal places in Kotlin, covering everything from simple string formatting to advanced BigDecimal approaches, alongside the latest Kotlin 2.1+ best practices and 2026 recommendations for production-grade code. Formatting doubles to 2 decimal places serves multiple critical purposes in application development. First, it ensures consistent presentation of financial data across user interfaces—displaying $19.50 instead of 19.499999999999998 prevents user confusion and maintains professional appearance. Second, it facilitates accurate rounding behavior; different rounding strategies (HALF_UP, FLOOR, CEILING) produce different results, and choosing the right approach impacts financial accuracy.
Find elsewhere
🌐
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 · 中文 – 简体
🌐
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-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 ...
🌐
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):
🌐
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 }...
🌐
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
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 ...
🌐
TutorialsPoint
tutorialspoint.com › kotlin-program-to-round-a-number-to-n-decimal-places
Kotlin Program to Round a Number to n Decimal Places
In this article, we will understand how to round a number to n decimal places. Rounding of decimal values are done using the ceil() or floor() functions. ... Step 2 ?
🌐
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?