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
🌐
Kotlin
kotlinlang.org › api › latest › jvm › stdlib › kotlin › to-big-decimal.html
toBigDecimal
December 7, 2021 - Try the revamped Kotlin docs design → · Try the revamped Kotlin docs design
🌐
Luasoftware
code.luasoftware.com › tutorials › kotlin › kotlin-convert-integer-to-bigdecimal-with-2-decimal-points
Kotlin Convert Integer to BigDecimal With 2 Decimal Points
Assuming you have integer 199, which you need to convert it to 1.99 in BigDecimal. ... 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 Kotlin Coroutine Scope for Activity, Fra
Discussions

How do you round a number to N decimal places - Support - Kotlin Discussions
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 More on discuss.kotlinlang.org
🌐 discuss.kotlinlang.org
2
August 2, 2018
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
math - Kotlin - How to eliminate decimals except the first two - Stack Overflow
When you convert it to a String, you may find that the number of decimal places has changed. To ensure a specific number of decimal places, you need BigDecimal. ... I have math result 3.5566 in float and i want to keep only the first 2 decimal to represent it as string, what the suitable way ... More on stackoverflow.com
🌐 stackoverflow.com
Fixing floating-point arithmetics with Kotlin
I am a little bit wary of "fixes" like this. Obviously it's surprising when you find out the variable you were expecting to be 0.2 is equal to 0.20000000000000018, but when you deal with floating-point arithmetic you need to understand these details. To illustrate, the first BigDecimal example has the same behavior because floats are still used - the author used the BigDecimal(double) constructor. However, JavaDoc clearly states this: The results of this constructor can be somewhat unpredictable. One might assume that writing new BigDecimal(0.1) in Java creates a BigDecimal which is exactly equal to 0.1 (an unscaled value of 1, with a scale of 1), but it is actually equal to 0.1000000000000000055511151231257827021181583404541015625. This is because 0.1 cannot be represented exactly as a double (or, for that matter, as a binary fraction of any finite length). Thus, the value that is being passed in to the constructor is not exactly equal to 0.1, appearances notwithstanding. On the other hand, once developers understand these nuances extension functions like .toBigDecimal() make a lot of sense. And Kotlin already provides the necessary operator functions: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/java.math.-big-decimal/ More on reddit.com
🌐 r/Kotlin
5
8
July 3, 2016
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.)

🌐
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 …
🌐
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 ... == 0.4) With setScale(), we specify the number of decimal places to which the Double must be rounded. To illustrate this, let’s set the scale to 2: val roundedUp = rawPositive.toBigDecimal().setScale(2, RoundingMode.UP).toDouble() assertTrue(roundedUp == 0.35) As shown, when we set the scale to 2, the number is rounded to two decimal places...
🌐
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 - Finally, we can convert the double value to a BigDecimal and restrict the double to 2-decimal points using the setScale() function with the specified RoundingMode. ... That’s all about rounding up a float or a double with 2 decimal places ...
🌐
Sling Academy
slingacademy.com › article › using-bigdecimal-for-high-precision-arithmetic-kotlin
Using `BigDecimal` for High-Precision Arithmetic in Kotlin - Sling Academy
Since BigDecimal can represent numbers with a lot of precision, dividing them without specifying a scale or rounding mode can throw an exception. The scale parameter (in this case, 2) ensures that the division operation results in two decimal places, and the rounding mode ROUND_HALF_UP specifies ...
Find elsewhere
🌐
Kotlin
kotlinlang.org › api › latest › jvm › stdlib › kotlin.text › to-big-decimal.html
toBigDecimal
Returns the Char that represents this decimal digit. Throws an exception if this value is not in the range 0..9. ... Returns the Char that represents this numeric digit value in the specified radix. Throws an exception if the radix is not in the range 2..36 or if this value is not in the range ...
🌐
Android Developers
developer.android.com › api reference › bigdecimal
BigDecimal  |  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 › 475345 › hi-all-i-m-having-some-problems-using-bigdecimal-i-m-not-sur
Hi all I m having some problems using BigDecimal I m not sur kotlinlang #getting-started
October 14, 2020 - Hi all, I'm having some problems using BigDecimal, I'm not sure if I'm missing anything or there is some issues with the toBigDecimal() method. I'm trying to create an expected value to validate a business logic. I'm trying the expected value to be a big decimal equals to 100.00 (note 2 decimal places...
🌐
SSOJet
ssojet.com › dev guides › binary-to-text encodings › decimal in kotlin
Decimal in Kotlin | Developer Guides
December 25, 2025 - Displaying BigDecimal values for users requires careful formatting to ensure readability and accuracy. You can achieve this using DecimalFormat or String.format(), which allow precise control over decimal places, currency symbols, and thousands separators.
🌐
MojoAuth
mojoauth.com › dev guides › kotlin bigdecimal: precise decimal math
Kotlin BigDecimal: Precise Decimal Math | MojoAuth Dev Guides
September 28, 2026 - Use BigDecimal in Kotlin for exact decimal math: create values with toBigDecimal, do arithmetic and rounding, and avoid floating-point errors with money.
🌐
Baeldung
baeldung.com › home › kotlin › kotlin numbers › dividing two int objects and getting a bigdecimal in kotlin
Dividing Two Int Objects and Getting a BigDecimal in Kotlin | Baeldung on Kotlin
September 7, 2023 - When we divide two Int objects in Kotlin, the result is automatically truncated to an integer value. This behavior can lead to inaccuracies when dealing with fractions, as any decimal portion of the result is discarded.
🌐
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 = Float.to_string(x, decimals: 2) Doc · S = io_lib:format("~.2f", [X]). write (unit=s,fmt="(F20.2)") x · import "fmt" s := fmt.Sprintf("%.2f", x) Demo · Doc · s <- showFFloat (Just 2) x "" num.toFixed(2) Demo · Doc · import java.math.BigDecimal; BigDecimal d = new BigDecimal(x); String s = "%.2f".formatted(d); Doc ·
🌐
GitHub
github.com › eobermuhlner › kotlin-big-math
GitHub - eobermuhlner/kotlin-big-math: Kotlin library for BigDecimal math functions (pow, sqrt, log, sin, ...) using arbitrary precision. · GitHub
This problem is solved in kotlin-big-math by providing a fun valueOf(Int): BigDecimal. // Code example with kotlin-big-math val n = 2 val v = valueOf(n)