3

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

Community
  • 1
  • 1
AgentKnopf
  • 4,295
  • 7
  • 45
  • 81
  • +1 for now wanting to use regexes! – fge Dec 28 '11 at 11:56
  • Thank you - yes using regex in this case can be a real pain and too much could go wrong (especially since there are numerous currencies and hence potential ways of user input) and I am sure there IS a clean solution to this, just can't seem to find it :) ... – AgentKnopf Dec 28 '11 at 12:01
  • Do you want to "10,40" and "10.40" be processed into 10.4 on the same device with the same locale? Or will it be "10,40" in, for example, German locale and "10.40" in, say, US locale? – ernazm Dec 28 '11 at 12:14
  • @emazm: It'll be locale based (I guess I have to trust the user on entering the currency that matches his locale...). I need 10.40 to parse it into a Number/BigDecimal, as 10,40 just wont parse. Nextr tricky thing is if the user (EU) enters 1.000,40 - as the dot is a (albeit optional) divider for thousands in EU currency. – AgentKnopf Dec 28 '11 at 12:23

3 Answers3

2

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.

ernazm
  • 9,208
  • 4
  • 44
  • 51
1

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);
Sunil Kumar Sahoo
  • 53,011
  • 55
  • 178
  • 243
  • Thank you for that suggestion - as I said in my original post, I actually tried "10,40 €" and it does not work - fails with unparsable number "10,40 €". I just tried it again to make sure. – AgentKnopf Dec 28 '11 at 12:19
  • The second question would be: How to reach from the user input (which is 10,40) to 10,40 € ? And I can't deduce from the input 10,40 that the user means Euro - can I? I guess not if there are potentially other currencies, which use the comma separator. Unless I check the locale I guess and make sure it's Euro. – AgentKnopf Dec 28 '11 at 12:21
  • Depending on locale you can append the currency notation to the string – Sunil Kumar Sahoo Dec 28 '11 at 12:46
  • Thank for the hint, but for instance in US currency u PRE-pend the currency to the String, right? Like this: $20.00 And I am afraid there is no way for me to know without checking (and knowing about) each and every currency if I have to prepend or append the currency symbol? – AgentKnopf Dec 28 '11 at 13:15
0

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
Dennis Jaheruddin
  • 21,208
  • 8
  • 66
  • 122
  • Thank you for the suggestion, however are u sure this would work internationally? I am not sure about the Yen, but I am sure it does not use "Cents" :) . However if there is no better, locale-based suggestion for parsing 10,40/10.40 I might consider this. Furthermore to convert it to a parseable currency value the divider for cents (to for instance reach the amount in dollar/euro/yen) would always have to be 100 in all currencies - right? – AgentKnopf Dec 28 '11 at 12:17
  • Though you have already found your answer I will reply here for completeness. There are some countries where the full currency is devided in a subcurrency that is not 1/100th of the main currency. I think this will not be a problem for japan as they use the full currency or sen, but here is a list of currencies and their subcurrency. Anything with 100 or none will be fine, but others like 1000 will cause a problem, so think carefully whether you want it to be compatible for those currencies. [Currency List](http://en.wikipedia.org/wiki/List_of_circulating_currencies) – Dennis Jaheruddin Jan 01 '12 at 14:54