This is the format you need:

val dec = DecimalFormat("#,###.##")

will print:

5.384,45

if you need always exactly 2 digits after the decimal point:

val dec = DecimalFormat("#,###.00") 
Answer from forpas on Stack Overflow
🌐
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?
🌐
Baeldung
baeldung.com › home › kotlin › kotlin numbers › number formatting in kotlin
Number Formatting in Kotlin | Baeldung on Kotlin
March 26, 2025 - As we can see, Kotlin’s String.format function is calling Java’s String.format method internally. Using this convenient extension function, we can create a similar function to accept the specified scale for the formatting: fun usingKotlinStringFormat(input: Double, scale: Int) = "%.${scale}f".format(input) ... val out0 = usingKotlinStringFormat(PI, 0) assertThat(out0).isEqualTo(EXPECTED_SCALE_0) val out2 = usingKotlinStringFormat(PI, 2) assertThat(out2).isEqualTo(EXPECTED_SCALE_2) val out4 = usingKotlinStringFormat(PI, 4) assertThat(out4).isEqualTo(EXPECTED_SCALE_4)
Discussions

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
Kotlin – String formatting - Stack Overflow
Kotlin has an excellent feature called string templates. val i = 10 val s = "i = $i" // evaluates to "i = 10" But is it possible to have any formatting in the templates? For e... More on stackoverflow.com
🌐 stackoverflow.com
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
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
🌐
Programming Idioms
programming-idioms.org › idiom › 23 › convert-real-number-to-string-with-2-decimal-places › 4693 › kotlin
Convert real number to string with 2 decimal places, in Kotlin
S = io_lib:format("~.2f", [X]). ... NumberFormat f = getNumberInstance(); f.setRoundingMode(HALF_UP); f.setMaximumFractionDigits(2); String s = f.format(x);
🌐
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?
🌐
Luasoftware
code.luasoftware.com › tutorials › kotlin › kotlin-string-literal-format-2-decimal-point
Kotlin String Literal Format 2 Decimal Point - Lua Software Code
December 28, 2018 - fun Double.formatDecimal(digits: Int) = "%.${digits}f".format(this)// PI=3.14val output = "$name=${value.formatDecimal(2)}" ... 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 this article. ... A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz ... Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan. ... Related articlesKotlin Properties Serialization (encode data class to map)Kotlin Convert Data Class to MapKotlin Serialization and Json (Android)Android
🌐
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
🌐
Spark By {Examples}
sparkbyexamples.com › home › kotlin › format string in kotlin with examples
Format String in Kotlin With Examples - Spark By {Examples}
May 9, 2024 - How to format a string in Kothin? Formatting string is a standard operation in most programming languages, including Kotlin. In Kotlin, you can use the
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

🌐
Reddit
reddit.com › r/kotlin › decimalformat for kotlin multiplatform
r/Kotlin on Reddit: DecimalFormat for Kotlin Multiplatform
September 21, 2020 -

Hello,

I'm trying to build a multiplatform library with Kotlin. The library I'm building deals with logic for formatting decimal. The only easy way I know for this would be to use Java DecimalFormat but since this is going to be multi-platform, using anything from Java is not possible. I noticed there is this open issue related to decimal formatting for Kotlin multiplatform: https://youtrack.jetbrains.com/issue/KT-21644?_ga=2.88225499.1133820560.1600725580-928101732.1566781632. So I guess currently there is no multiplatform library for decimal format? If you happened to run into this, what was your work around? Did you end up having to create your own library from scratch?

🌐
Lets-plot
lets-plot.org › kotlin › formats.html
Formatting | Lets-Plot for Kotlin
March 20, 2026 - format number result .1f 0.42 "0.4" 0,.1f 1234567.89 "1,234,567.9" +$,.2f 1e4 "+$10,000.00" +$,.2~f 1e4 "+$10,000" ~g 0.0000042 "0.0000042" ~g 0.00000042 "4.2e-7" ~g 420000 "420000" ~g 4200000 "4.2e+6" ,.2g -4231 "-4,2e+3" ,.6g -4231 "-4,231.00" ,.6~g -4231 "-4,231" See more examples here. The number format can be used in a template to create a string with variable substitution.
🌐
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 ...
🌐
JetBrains
youtrack.jetbrains.com › issue › KT-21644 › Add-utility-function-for-formatting-of-decimal-numbers-to-Kotlin-common-library
Add utility function for formatting of decimal numbers to ...
{{ (>_<) }} This version of your browser is not supported. Try upgrading to the latest stable version. Something went seriously wrong
🌐
Programiz
programiz.com › kotlin-programming › examples › round-number-decimal
Kotlin Program to Round a Number to n 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)) }
🌐
sebhastian
sebhastian.com › kotlin-string-format
Kotlin String format() method example | sebhastian
January 3, 2022 - Kotlin borrows the String.format() method from the Java language, so you can format your string values with it. For example, suppose you want to format the PI value into a two-digit format (3.14) ... val PI = 3.14159265358979323 val myStr = String.format("The PI value is %.2f", PI) print(myStr)// The PI value is 3.14
🌐
Kotlin Discussions
discuss.kotlinlang.org › support
Convert double to string to double - Support - Kotlin Discussions
August 11, 2019 - How can I convert a double to a string using the country’s formatting and back again to a string? For example if the number is Pi and I’m in France, I want the conversion to string to produce “3,14” with rounding of 2 decimal places and then convert “3,14” back to the rounded floating ...
🌐
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....