view raw
patrick patrick - 6 months ago 59
Android Question

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?


I believe I have a way. I've never USED this, mind you, but it should certainly get you close, if not there:

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


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.