Search code examples
androiddecimal-point

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?


Solution

  • 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.