AgentKnopf
AgentKnopf

Reputation: 4335

Android: Parsing EU currency into a Number

I have an EditText which should work as follows (Android 2.3.3):

The user enters for instance 10,40 (which is 10 Euro and 40 Cents in EU currency) - I want to then take that String input and convert it to a BigDecimal (or Number etc...) for instance, for further internal computation.

The same should work if the user enters 10.40 (which - in the case of US Dollar would amount to 10 Dollars and 40 Cents), then take that String input and convert it to a BigDecimal (or Number etc...) for instance.

I tried the following (based on Michael Petrotta's answer here), which fails with "ParseException: Unparsable Number 10,40):

    // German input
    NumberFormat formatter = NumberFormat.getCurrencyInstance(Locale.GERMANY);
    // User input
    String value = "10,40";
    Number valueParsed = formatter.parse(value);

I also tried "10,40 €" to no avail. So either I am misunderstanding the purpose of NumberFormat.parse or I'm on a wrong track all together...? I really want to avoid using Regular Expressions to replace the comma in "10,40" for the version should work internationally and I'd like some clean, Locale-based approach.

EDIT

I found this question which addresses the same issue - the correct answer uses the same approach that I do, so I tried again (using France as a Locale this time) but it fails nonetheless, which seems quite odd... The comment under the referenced question suggested using replaceAll(",", ".") which seems a bad idea to me, as some currencies use the comma as a divider for thousands, such as the US Dollar if I am not mistaken. I could however check the locale but that all seams shady :( and potentially erroneous ...

Upvotes: 3

Views: 3031

Answers (3)

ernazm
ernazm

Reputation: 9258

It seems that you don't really need currency format. Use NumberFormat.getNumberInstance() instead:

String value = "10,40";
double d = (Double)NumberFormat.getNumberInstance().parse(value);
BigDecimal price = new BigDecimal(d);

This will throw NumberFormatException though in case the user will enter the value in format inappropriate for his locale (e.g. "10,40" for US), but should work in regular case.

Upvotes: 2

Sunil Kumar Sahoo
Sunil Kumar Sahoo

Reputation: 53657

You are missing currency notation for that locale in your string.

eg for Germany currency notation is €

your user inserted data should be String value = "10,40 €"; instead of String value = "10,40"

Following example works fine

NumberFormat formatter1 = NumberFormat.getCurrencyInstance(Locale.GERMANY);
            // User input
            String value = "10,40 €";
            Number valueParsed = formatter1.parse(value);
            System.out.println(valueParsed);

Upvotes: 1

Dennis Jaheruddin
Dennis Jaheruddin

Reputation: 21563

Unfortunately I cannot give you more insight on using Locale, but to answer the other part of your question: If you want something that works internationally just follow this:

Strip everything but numbers and separators.

If there is a separator on the third place from the right?
 Strip all separators and you have the desired amount in cents
Else
 Strip all separators, multiply by 100 and you have the desired amount in cents

Upvotes: 0

Related Questions