2

I'm using NumberFormat in my app to get the currency formatted Strings. Like if a user inputs 12.25 in the field then it will be changed to $12.25 based on locale. Here the Locale is en-US. Now I want to get the 12.25 value as double form the formatted string. To do that I have used:

NumberFormat.getCurrencyInstance().parse("$12.25").doubleValue();

Above line giving me the result of 12.25 which is my requirement. But suppose a user changed his locale to something else en-UK. Now for that locale above statement is giving me parseException. Because for the locale en-UK, currency string $12.25 is not parsable.

So is there any way to get the double value from currency formatted string irrespective of what the locale is?

Deepak Goyal
  • 4,747
  • 2
  • 21
  • 46

3 Answers3

1

I don't know either the below solution is perfect or not but it is working according to my requirement.

try {
    return NumberFormat.getCurrencyInstance().parse(currency).doubleValue();
} catch (ParseException e) {
    e.printStackTrace();
    // Currency string is not parsable
    // might be different locale
    String cleanString = currency.replaceAll("\\D", "");
    try {
        double money = Double.parseDouble(cleanString);
        return money / 100;
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
return 0;
Deepak Goyal
  • 4,747
  • 2
  • 21
  • 46
0

What about

new Double(NumberFormat.getCurrencyInstance().parse("$12.25").doubleValue());

and also you could use

Double.valueOf() creates a Double object so .doubleValue() should not be necessary.

also Double.parseDouble(NumberFormat.getCurrencyInstance().parse("$12.25"));

could work

Gastón Saillén
  • 12,319
  • 5
  • 67
  • 77
  • thanks for your response. But my question is parsing doesn't work if the user locale and currency formatted string locale are not matched. So I need a solution to that. – Deepak Goyal Mar 21 '18 at 15:54
  • 1
    you could just do a string for only the locale with the currency and attach it to the value – Gastón Saillén Mar 21 '18 at 15:55
  • @DeepakGoyal Yup...You can't hardcode only en-US Locale and expect it to work with en-UK Locale – Jason Krs Mar 21 '18 at 15:59
  • @JasonKrs Exactly, this is my requirement. – Deepak Goyal Mar 21 '18 at 16:00
  • @DeepakGoyal I guess programmatically speaking, you gotta adapt your code to your requirements.... If you program is going to be used is 5 different Locale, then I advise you SWITCH CASE the heck out of it ;-) – Jason Krs Mar 21 '18 at 16:02
  • @DeepakGoyal unless you always split the first character and do your work just like Antonio said in the first command – Jason Krs Mar 21 '18 at 16:03
  • @JasonKrs Some currencies have two chars like `R$` and in this currency, the comma is used for decimal and dot is used for separation. – Deepak Goyal Mar 21 '18 at 16:21
0

Here's a little algorithm that may help you :

public static void main(String[] args)  {

        String cash = "R$1,000.75"; //The loop below will work for ANY currency as long as it does not start with a digit
        boolean continueLoop = true;

                char[] cashArray = cash.toCharArray();      

        int cpt = 0;

                while(continueLoop){
                    try  
                    {  
                        double d = Double.parseDouble(cashArray[cpt]+"");  
                        continueLoop = false;
                    }catch(NumberFormatException nfe){  
                        cpt += 1;  
                    }
                }

                System.out.println(cpt);

                //From here you can do whatever you want....

                String extracted = cash.substring(cpt);

                NumberFormat format = NumberFormat.getInstance(Locale.US); //YOUR REQUIREMENTS !!!! lol
                try {
                    Number youValue = format.parse(extracted);
                    System.out.println(youValue.doubleValue());
                } catch (ParseException ex) {
                    //handle your parse error here
                }

    }

You should get as result here in the output:

2

1000.75

Jason Krs
  • 704
  • 2
  • 9
  • 30