Change DecimalFormat locale
Asked Answered
T

7

66

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.

Tantra answered 5/4, 2016 at 6:41 Comment(3)
Try DecimalFormat df = DecimalFormat.getInstance(Locale.US);Chuipek
thank but i how i can use locale with this pattern ##.########? @VirenTantra
Try DecimalFormat formater = DecimalFormat.getInstance(Locale.US); formater.applyPattern("#.##");Chuipek
D
51

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);
Dangerfield answered 11/6, 2017 at 3:44 Comment(2)
Also in the documentation. docs.oracle.com/javase/tutorial/i18n/format/decimalFormat.htmlWalt
That's Java's java.text.DecimalFormat. The question is about Android's android.icu.text.DecimalFormat.Johnstone
S
143

You can specify locale for DecimalFormat this way:

DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.US);
DecimalFormat format = new DecimalFormat("##.########", symbols);
Sebastien answered 6/6, 2018 at 10:40 Comment(2)
should be ACCEPTED answer!Mournful
yeah it should beStearns
D
51

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);
Dangerfield answered 11/6, 2017 at 3:44 Comment(2)
Also in the documentation. docs.oracle.com/javase/tutorial/i18n/format/decimalFormat.htmlWalt
That's Java's java.text.DecimalFormat. The question is about Android's android.icu.text.DecimalFormat.Johnstone
L
27

You can use basic constructor for setting Locale while creating DecimalFormat object:

DecimalFormat dFormat = new DecimalFormat("#.#", new DecimalFormatSymbols(Locale.US));
Literary answered 12/2, 2019 at 13:23 Comment(0)
S
1

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

Stearns answered 2/3, 2022 at 9:9 Comment(0)
A
0

Use simply this method to convert current localization wise number,

public static String currencyFormatter(String balance) {
    try {
        double amount = Double.parseDouble(balance);
        DecimalFormat decimalFormat = new DecimalFormat("##,##,##,###.##");
        DecimalFormat locationSpecificDF = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            locationSpecificDF = (DecimalFormat) DecimalFormat.getNumberInstance(Locale.forLanguageTag("bn")); // Ex. en, bn etc.
        } else {
            return decimalFormat.format(amount);
        }
        return locationSpecificDF.format(amount);
    } catch (Exception e) {
        return balance;
    }
}

or follow this link.

Algetic answered 18/9, 2018 at 9:28 Comment(0)
B
0

if you want to use only NumberFormat, you can do this way:

unicoinsAmmount.setText(NumberFormat.getNumberInstance(Locale.US).format(vc));

Bobbyebobbysocks answered 27/11, 2018 at 11:29 Comment(0)
K
0

You can specify the number of fraction digit after the decimal point, and/or the numbers before the decimal point. Of-course, setting the current locale is also important.

   private String formatNumber(double number) {

    NumberFormat nf = NumberFormat.getNumberInstance(Locale.getDefault());
    if (nf instanceof DecimalFormat) {
        try {
            DecimalFormat formatter = (DecimalFormat) nf;
            formatter.setDecimalSeparatorAlwaysShown(true);
            formatter.setMinimumFractionDigits(2);
            formatter.setMaximumFractionDigits(2);
            return formatter.format(new BigDecimal(number);
        } catch (NumberFormatException nfe) {
            nfe.printStackTrace();
        }
    }
    return null;
}
Kraken answered 19/1, 2020 at 0:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.