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.
Decimal format in a multi-platform build - Libraries - Kotlin Discussions
java - Kotlin. How to format decimal number with zero at the end? - Stack Overflow
DecimalFormat and NumberFormat libs for Kotlin Multiplatform ?
DecimalFormat for Kotlin Multiplatform
Remove last two # after . and add 00 in place of ##.
val dec = DecimalFormat("#,###,##0.00")
Example:
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.*;
public class AddZeroToOneDigitDecimal{
public static void main(String []args){
System.out.println(customFormat(3434));
System.out.println("----------");
System.out.println(customFormat(3434.34));
System.out.println("----------");
System.out.println(customFormat(3434.3));
}
public static String customFormat(double d){
String result = formatter.format(d);
return (result.replace(".00",""));
}
private static DecimalFormat formatter;
private static final String DECIMAL_FORMAT = "#,###,##0.00";
private static DecimalFormatSymbols formatSymbols = new DecimalFormatSymbols(Locale.ENGLISH);
static {
formatSymbols.setDecimalSeparator('.');
formatSymbols.setGroupingSeparator(' ');
formatter = new DecimalFormat(DECIMAL_FORMAT, formatSymbols);
}
}
Simplified
val formatter = DecimalFormat("#,###,##0.00");
return formatter.format(value).replace(".00","");
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?
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.
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
The code below was tested against all your examples and seemed to work well:
val locale = Locale("en", "UK")
val symbols = DecimalFormatSymbols(locale)
symbols.decimalSeparator = ','
val pattern = "#.##"
val decimalFormat = DecimalFormat(pattern, symbols)
val format = decimalFormat.format(3.14)
println(format) //3,14
To set a specific separator in your DecimalFormat, you can use setDecimalSeparator.
Pay attention to the pattern as # means:
A digit, leading zeroes are omitted
You can obviously change the locale to your fitting.
More information here.
You indeed might use java.text.NumberFormat to achieve your goal. The following should work is quite close to your example Swift code.
// you can change the separators by providing a Locale
val nf = java.text.NumberFormat
.getInstance(java.util.Locale.GERMAN)
nf.minimumFractionDigits = 0
nf.maximumFractionDigits = 2
// you may want to change the rounding mode
nf.roundingMode = java.math.RoundingMode.DOWN
println(nf.format(0)) // 0
println(nf.format(1)) // 1
println(nf.format(1.2)) // 1,2
println(nf.format(1.23)) // 1,23
println(nf.format(1.234)) // 1,23
println(nf.format(12.345)) // 12,34