Decimal point or decimal comma in Android

patrick picture patrick · Nov 18, 2011 · Viewed 11.8k times · Source

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

Answer

Kevin Coppock picture Kevin Coppock · Nov 18, 2011

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.