Search code examples
javamathlocale

Is locale aware parsing for Numberformat possible in Android?


I have an Android application which does some basic mathematics.

Example

  try {
        NumberFormat nf = NumberFormat.getNumberInstance();
        DecimalFormat df = (DecimalFormat) nf;
        a = Float.parseFloat(vw3.getText().toString());
        f = Float.parseFloat(vw5.getText().toString());

        c = a / 100;
        d = c * 1.036f;
        e = f / 100;
        g = e * 1.24f;
        h = d + g;

        String str1 = String.valueOf(df.format(h));
        vw7.setText(str1);
    } catch (NumberFormatException f) {
        a = (0);
    }
}

When the user is in the USA the calculations work fine and format fine. Well as you would expect. The 1,000.00 format where the grouping is by comma and separator is by decimal point. When a user is in France, the grouping is different and the separator is also different. Using the 1,000.00 example, in France the number would be formatted like this 1 000,00. A space is the grouping separator and the comma is the decimal separator. This causes a problem when you try and run a calculation and you will get a NumberFromatException (NFE). And I anticipated a NFE issue and catch it and replace the possible cause with the correct number. However, replacing any comma with a space and any period with a comma will also produce a NFE.

Example

 try {
        NumberFormat nf = NumberFormat.getNumberInstance();
        DecimalFormat df = (DecimalFormat) nf;
        a = Float.parseFloat(vw3.getText().toString().replace(",",""));
        f = Float.parseFloat(vw5.getText().toString().replace(",",""));

        c = a / 100;
        d = c * 1.036f;
        e = f / 100;
        g = e * 1.24f;
        h = d + g;

        String str1 = String.valueOf(df.format(h));
        vw7.setText(str1);
    } catch (NumberFormatException f) {
        a = (0);
    }
}

EDIT - As suggested by Peter O. I have tried parsing the number with a locale aware means.

Example

NumberFormat.getNumberInstance().parse(string);

Or

NumberFormat df = NumberFormat.getNumberInstance();
        String value = "10,40 €";
        Number valueParsed = df.parse(value);
        vw7.setText(valueParsed);

Will produce a "Bad Class" illegalargument.

I am looking for a solution to where I can do the calculations in an acceptable manner within the apps programming regardless of the locale and then later format the results to the locale. The question could be or is, do you force a locale for your calculations and then format the results for the locale?


Solution

  • If this is the code you are using and your strings will have the currency symbol. In this case € the EURO symbol.

    Your example:

    NumberFormat df = NumberFormat.getNumberInstance();
            String value = "10,40 €";
            Number valueParsed = df.parse(value);
            vw7.setText(valueParsed);
    

    so that value always has a currency symbol you need to use

    NumberFormat df= NumberFormat.getCurrencyInstance();
    

    instead of the .getNumberInstance(). This worked on my system which is currently set so that the € symbols is the default currency symbol. You will have to try it on a system that has the $ currency symbol in order to verify that it works there, also.