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 OverflowThis 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")
The "most Kotlin-esque" way I found to do this sort of formatting is:
"%,.2f".format(Locale.GERMAN, 1234.5678) // => "1.234,57"
"%,.2f".format(Locale.ENGLISH, 1234.5678) // => "1,234.57"
"%,.2f".format(1234.5678) // => "1,234.57" for me, in en_AU
Note though that even though this is Kotlin's own extension method on String, it still only works on the JVM.
For those looking for a multiplatform implementation (as I was), mp_stools is one option.
DecimalFormat and NumberFormat libs for Kotlin Multiplatform ?
DecimalFormat for Kotlin Multiplatform
Decimal format in a multi-platform build - Libraries - Kotlin Discussions
Format a double to fixed decimal length - Support - Kotlin Discussions
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,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
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?