0

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?

Mr. Ed
  • 74
  • 1
  • 4
  • 17
  • 1
    `Float.parseFloat` and `String.valueOf` always use the same formatting convention regardless of locale. You need to use a locale-aware number parser. – Peter O. May 24 '17 at 22:21
  • In fact, this question appears to be specific to Java, not Android. – Peter O. May 24 '17 at 22:22
  • Possible duplicate of [Best way to parseDouble with comma as decimal separator?](https://stackoverflow.com/questions/4323599/best-way-to-parsedouble-with-comma-as-decimal-separator) – Peter O. May 24 '17 at 22:24
  • I have tried "myNumber = df.parse(myString);" and it returns an error of "Bad Class". Hence the question. Thanks for your response. – Mr. Ed May 24 '17 at 22:26
  • Illegal argument is due to the euro sign. – Norbert May 24 '17 at 22:50

1 Answers1

0

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.

Barns
  • 4,850
  • 3
  • 17
  • 31
  • Good catch! This does work on my system based on decimal separators. And does answer my first question. So the solution for me is parse the numbers with locale aware parsing and format the results for display. – Mr. Ed May 24 '17 at 23:12
  • use `String val = df.format(valueCalculated);` to get a string representation with currency symbol of your calculated result which you can then display to the user. – Barns May 24 '17 at 23:24