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
🌐
Baeldung
baeldung.com › home › kotlin › kotlin numbers › number formatting in kotlin
Number Formatting in Kotlin | Baeldung on Kotlin
March 26, 2025 - It’s worth mentioning that, in the usingJavaStringFormat method, we’ve used Kotlin’s String template to set the scale: ${scale}. In fact, the standard printf format string’s syntax supports ‘*‘ to define a dynamic width. Let’s take shell’s printf command to show some examples: $ printf "%.*f\n" 0 3.1415926 3 $ printf "%.*f\n" 2 3.1415926 3.14 $ printf "%.*f\n" 4 3.1415926 3.1416 · However, Java’s format method doesn’t support using ‘*’ as the placeholder for a dynamic width in the format string.
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
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
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
🌐
Programiz
programiz.com › kotlin-programming › examples › round-number-decimal
Kotlin Program to Round a Number to n Decimal Places
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 ...
🌐
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 …
🌐
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?
🌐
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?
🌐
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....
🌐
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
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

🌐
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
🌐
Kotlin Discussions
discuss.kotlinlang.org › libraries
Decimal format in a multi-platform build - Libraries - Kotlin Discussions
December 6, 2017 - I am trying to converting one of my JavaFX projects to a multi-platform one. Since I do not have any experience with javascript ecosystem it is moving very slow. One specific problem I encountered, that there seems to be no way to define a format for numbers in common library.
🌐
SSOJet
ssojet.com › binary-encoding-decoding › decimal-in-kotlin
Decimal in Kotlin | Binary to Text Encodings in Programming Languages
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.
🌐
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 ? Declare a float value namely myInput. ... Step 4 ? Use format() to alter the number of decimal places required.
🌐
Lets-plot
lets-plot.org › kotlin › formats.html
Formatting | Lets-Plot for Kotlin
1 month ago - %m - month as a zero-padded decimal number (01, 02, …, 12);
🌐
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?

🌐
Reddit
reddit.com › r/kotlin › decimalformat and numberformat libs for kotlin multiplatform ?
r/Kotlin on Reddit: DecimalFormat and NumberFormat libs for Kotlin Multiplatform ?
April 14, 2023 -

I'm working on a multiplatform library with Kotlin

We need to display numbers with either "comma decimal " and "dot decimal" formats based on the locale

For example

  1. 1,00,000.50 for India , 1.00.00,50 for Europe

For this purpose I used Java's number format and Decimal format classes

DecimalFormat decim = new DecimalFormat("#,###.##"); tv.setText(decim.format(someFloat));

OR

NumberFormat.getInstance().format(my number) 

Both are good, but they are JAVA libraries won't work in KMM code

So, my question, is there any DecimalFormat and NumberFormat alternate libraires available for Kotlin Multiplatform ?

I don't like to use Expect / Actual implementation but a native ones

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.)

🌐
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 this article.