Using a DecimalFormat is a perfectly good way to format a number.

However, you have a deeper issue here, which is that you should never use a Float or Double to store a value that needs to be exact, such as money.

That's because Floats and Doubles are stored as binary fractions, not decimal fractions.  And just as you can't represent 1/3 exactly as a decimal fraction of any finite length (0.33333333333…), so you can't represent 1/10 exactly as a binary fraction (0.0001100110011…).  So most of the decimal numbers you want to store will get approximated and rounded to the nearest binary fraction that can be stored.  This isn't always obvious — when printing them out, they get rounded again to the nearest decimal fraction, and in many cases that ‘recovers’ the number you want — but there are many cases where it's noticeable, especially as the result of calculations.

You can see the effect in the Kotlin REPL:

>>> 0.1 + 0.2
res0: kotlin.Double = 0.30000000000000004

In this case, the binary fractions nearest to 0.1 and 0.2 sum to give a binary fraction that's nearer to 0.30000000000000004 than it is to 0.3.

(There are many existing questions on StackOverflow discussing this, such as here.)

So if you need your money values to be accurate (and you almost always do!), then you should store them some other way.  For example, if you only ever need two decimal places (i.e. the number of paise), then simply store the number of paise as an integer.  Or if you don't need to do any calculations, you could store the number as a string (which is otherwise a bad idea…).

However, the most general and flexible way in Kotlin (and Java) is to use BigDecimal.  That uses decimal fractions internally to represent any decimal number exactly, to any precision you need, and you can easily do calculations and other manipulations.

In Java, using it is awkward and long-winded, but Kotlin's operator overloading makes it very natural, e.g.:

>>> val p1 = "0.1".toBigDecimal()
>>> val p2 = "0.2".toBigDecimal()
>>> p1 + p2
res3: java.math.BigDecimal = 0.3

DecimalFormat supports it too:

>>> java.text.DecimalFormat("#,##0.00").format(p1 + p2)
res4: kotlin.String! = 0.30

(Note: do not create a BigDecimal from a Float or Double, as the damage will already have been done.  If you have an integer value, then start from an integer type such as Int or Long; otherwise, you'll need to start from a String to get an exact value.)

Answer from gidds on Stack Overflow
🌐
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 - ... Alternatively, we can call the DecimalFormat.format() function to restrict the double to 2-decimal points using the pattern #.##. The RoundingMode can be provided using the setRoundingMode() function.
🌐
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
Discussions

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
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 - How to format numbers with two decimal places? - Stack Overflow
This is what I am doing to display the amount with two decimal places. It is working fine, but I would like to know if this is right or is there any drawback in doing so and what is the better way ... More on stackoverflow.com
🌐 stackoverflow.com
android - Format number using decimal format in kotlin - Stack Overflow
I am facing an issue where I need to do some calculations with a number like for example 5000,00 multiplied it by (1,025^3). So in this case 5000,00 * (1,025^3) = 5385,45 So my question is, how c... More on stackoverflow.com
🌐 stackoverflow.com
🌐
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....
🌐
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)
🌐
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?
🌐
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 ...
🌐
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?
Top answer
1 of 1
9

Using a DecimalFormat is a perfectly good way to format a number.

However, you have a deeper issue here, which is that you should never use a Float or Double to store a value that needs to be exact, such as money.

That's because Floats and Doubles are stored as binary fractions, not decimal fractions.  And just as you can't represent 1/3 exactly as a decimal fraction of any finite length (0.33333333333…), so you can't represent 1/10 exactly as a binary fraction (0.0001100110011…).  So most of the decimal numbers you want to store will get approximated and rounded to the nearest binary fraction that can be stored.  This isn't always obvious — when printing them out, they get rounded again to the nearest decimal fraction, and in many cases that ‘recovers’ the number you want — but there are many cases where it's noticeable, especially as the result of calculations.

You can see the effect in the Kotlin REPL:

>>> 0.1 + 0.2
res0: kotlin.Double = 0.30000000000000004

In this case, the binary fractions nearest to 0.1 and 0.2 sum to give a binary fraction that's nearer to 0.30000000000000004 than it is to 0.3.

(There are many existing questions on StackOverflow discussing this, such as here.)

So if you need your money values to be accurate (and you almost always do!), then you should store them some other way.  For example, if you only ever need two decimal places (i.e. the number of paise), then simply store the number of paise as an integer.  Or if you don't need to do any calculations, you could store the number as a string (which is otherwise a bad idea…).

However, the most general and flexible way in Kotlin (and Java) is to use BigDecimal.  That uses decimal fractions internally to represent any decimal number exactly, to any precision you need, and you can easily do calculations and other manipulations.

In Java, using it is awkward and long-winded, but Kotlin's operator overloading makes it very natural, e.g.:

>>> val p1 = "0.1".toBigDecimal()
>>> val p2 = "0.2".toBigDecimal()
>>> p1 + p2
res3: java.math.BigDecimal = 0.3

DecimalFormat supports it too:

>>> java.text.DecimalFormat("#,##0.00").format(p1 + p2)
res4: kotlin.String! = 0.30

(Note: do not create a BigDecimal from a Float or Double, as the damage will already have been done.  If you have an integer value, then start from an integer type such as Int or Long; otherwise, you'll need to start from a String to get an exact value.)

Find elsewhere
🌐
Kotlinlang
slack-chats.kotlinlang.org › t › 542693 › hello-it-might-be-trivial-but-how-to-format-a-double-to-two-
hello it might be trivial but how to format a double to two kotlinlang #multiplatform
July 8, 2022 - hello it might be trivial but how to format a double to two decimal points like 1 123456 gt 1 12 so the decimal point is marked with ` ` with pure kotlin I can see that ` format` in kotlin stdlib uses
🌐
Alvin Alexander
alvinalexander.com › java › how-round-double-float-two-decimal-places-format-output
Java/Scala/Kotlin: How to round a double (or float) to two decimal places? (formatting output) | alvinalexander.com
July 28, 2024 - The same solution works for Scala and Kotlin as well. In summary, if you wanted to see solutions for how to round Java double and float values to two decimal places (and other decimal places), including how to round numbers up and down, I hope these examples are helpful.
🌐
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 ·
🌐
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 · 中文 – 简体
🌐
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
🌐
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?

🌐
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.
Top answer
1 of 2
2

Try multiplying the number by 10 until you know you got to two non zero digits. This way you will get the number of decimal points you have to round up.

After that you use the NumberFormat to format the number like this:

Use with AIval format = NumberFormat.getInstance()
format.maximumFractionDigits = nrOfDecimalPoints
val formattedNumber = format.format(yourNumber)

This way you will keep the thousands separator "," and the decimal will be cut off after two non zero digits.

EDIT

This is how the custom function looks like:

Use with AIprivate fun customFormat(number: Double): String{
    var temp = number - number.toInt()
    temp *= 100
    var nrDec = 2
    if (number <= 0.0){
        return "0.00"
    }
    if (temp >= 1.0){
        val format = NumberFormat.getInstance(Locale.US)
        format.maximumFractionDigits = nrDec
        return format.format(number)
    }
    while (temp < 1.0 && nrDec < 15){
        temp *= 10
        nrDec ++
    }
    if((temp * 10 % 10) != 0.0){
        nrDec++
    }
    val format = NumberFormat.getInstance(Locale.US)
    format.maximumFractionDigits = nrDec
    return format.format(number)
}
2 of 2
1

You can do it like this in order to cut of tailing 0:

Use with AIpublic static void main(String []args){
    double number1 = 0.2;
    double number2 = 1.55;
    double number3 = 0.00005;
    double number4 = 0.50000;
    
    DecimalFormat df = new DecimalFormat("###,##0.00#########");
    System.out.println(df.format(number1)); // 0.20
    System.out.println(df.format(number2)); // 1.55
    System.out.println(df.format(number3)); // 0.00005
    System.out.println(df.format(number4)); // 0.50
 }

You just need to know how far your decimal digits should be checked.

🌐
Luasoftware
code.luasoftware.com › tutorials › kotlin › kotlin-string-templates-format-double
Kotlin String Templates Format Double with Decimals
Use Kotlin String.format instead. ... 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.