patrick
patrick

Reputation: 1292

Decimal point or decimal comma in Android

Is there an easy way to read back if the language set uses a decimal comma or a decimal point?

Upvotes: 25

Views: 16130

Answers (5)

Kevin Coppock
Kevin Coppock

Reputation: 134714

EDIT: Updating based on @Algar's suggestion; you can directly use:

char separatorChar = DecimalFormatSymbols.getInstance().getDecimalSeparator();

As it will always return an instance of DecimalFormatSymbols.


NumberFormat nf = NumberFormat.getInstance();
if (nf instanceof DecimalFormat) {
    DecimalFormatSymbols sym = ((DecimalFormat) nf).getDecimalFormatSymbols();
    char decSeparator = sym.getDecimalSeparator();
}

Docs:

NumberFormat, DecimalFormat, DecimalFormatSymbols

According to the DecimalFormat docs, apparently calling NumberFormat.getInstance() is safe, but may return a subclass other than DecimalFormat (the other option I see is ChoiceFormat). I believe for the majority of instances it should be a DecimalFormat, and then you can compare decSeparator against a , and . to see which format it is using.

Upvotes: 35

Algar
Algar

Reputation: 5984

Or why not

DecimalFormatSymbols.getInstance().decimalSeparator

Upvotes: 8

Milos Simic Simo
Milos Simic Simo

Reputation: 169

you can use:

Currency currency = Currency.getInstance(device_locale);

than use currency.getSymbol() for symbol. For default device locale you can use:

@TargetApi(Build.VERSION_CODES.N)
    public static Locale getCurrentLocale(Context c) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            return c.getResources().getConfiguration().getLocales().get(0);
        } else {
            //noinspection deprecation
            return c.getResources().getConfiguration().locale;
        }
    }

Upvotes: 1

user462990
user462990

Reputation: 5532

I did try this and it worked fine...

    String osVersion = System.getProperty("os.version");
String PhoneModel = android.os.Build.MODEL;
String locale = this.getResources().getConfiguration().locale.getDisplayCountry();
char decSeparator = '*';
DecimalFormatSymbols dfs = new DecimalFormatSymbols();
decSeparator = dfs.getDecimalSeparator();
String androidVersion = android.os.Build.VERSION.RELEASE;
String prologue = String.format("OS verson = %s PhoneModel = %s locale = %s DecimalFormatSymbol = [%c] androidVersion = %s ", 
        osVersion ,PhoneModel, locale, decSeparator,androidVersion);

Upvotes: 2

prolink007
prolink007

Reputation: 34594

Not sure if there is an easy way, but you could test which language set is being used and then make the appropriate changes according to if that language used commas or decimals.

Upvotes: -1

Related Questions