Change DecimalFormat locale
35,331
Solution 1
You may try by first converting to NumberFormat
and then Cast it to DecimalFormat
Integer vc = 3210000;
NumberFormat nf = NumberFormat.getNumberInstance(Locale.US);
DecimalFormat formatter = (DecimalFormat) nf;
formatter.applyPattern("#,###,###");
String fString = formatter.format(vc);
return convertNumbersToEnglish(fString);
Solution 2
You can specify locale for DecimalFormat
this way:
DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.US);
DecimalFormat format = new DecimalFormat("##.########", symbols);
Solution 3
You can use basic constructor for setting Locale while creating DecimalFormat object:
DecimalFormat dFormat = new DecimalFormat("#.#", new DecimalFormatSymbols(Locale.US));
Solution 4
you can create an extension function like below
fun Double.toRate(): String? {
val symbols = DecimalFormatSymbols(Locale.US)
val decimalFormat = DecimalFormat("##.######", symbols)
decimalFormat .minimumFractionDigits = 2
return decimalFormat .format(this)
}
Thanks
Related videos on Youtube
Author by
BekaKK
Updated on March 02, 2022Comments
-
BekaKK about 2 years
I have custom
DecimalFormat
in Edittext's addTextChangedListener method, everything is working perfectly but when I change language (locale) my addTextChangedListener is not working.double answer = inputDouble * counterToDouble; DecimalFormat df = new DecimalFormat("##.########"); // df=(DecimalFormat)numberFormat; df.setRoundingMode(RoundingMode.DOWN); answer = Double.parseDouble(df.format(answer)); unicoinsAmmount.setText(String.valueOf(df.format(answer)));
I searched about my problem and found a
NumberFormat
solution:NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.US);
but I don't know how I can use this code.
-
Admin about 8 yearsTry DecimalFormat df = DecimalFormat.getInstance(Locale.US);
-
BekaKK about 8 yearsthank but i how i can use locale with this pattern ##.########? @Viren
-
Admin about 8 yearsTry DecimalFormat formater = DecimalFormat.getInstance(Locale.US); formater.applyPattern("#.##");
-
-
OneCricketeer almost 7 yearsAlso in the documentation. docs.oracle.com/javase/tutorial/i18n/format/decimalFormat.html
-
Alessandro Scarozza over 5 yearsshould be ACCEPTED answer!
-
Agent_L almost 3 yearsThat's Java's
java.text.DecimalFormat
. The question is about Android'sandroid.icu.text.DecimalFormat
. -
Abhijith Brumal about 2 yearsyeah it should be