Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert currency formatted String back to a BigDecimal? (using Java NumberFormat)

In my Android app I've got an EditText from which I take a number, and convert that to a BigDecimal, and from there to a local Currency formatting:

String s = "100000";
Locale dutch = new Locale("nl", "NL");
NumberFormat numberFormatDutch = NumberFormat.getCurrencyInstance(dutch);
Log.e(this, "Currency Format: "+ numberFormatDutch.format(new BigDecimal(s.toString())));

This prints out €100.000,00 like expected. I now however, want to convert this back into a BigDecimal.

Is there a way that I can convert a locally formatted currency string back to a BigDecimal?

like image 605
kramer65 Avatar asked Mar 22 '23 10:03

kramer65


1 Answers

    String s = "100000";
    Locale dutch = new Locale("nl", "NL");
    NumberFormat numberFormatDutch = NumberFormat.getCurrencyInstance(dutch);

    String c = numberFormatDutch.format(new BigDecimal(s.toString()));
    System.out.println("Currency Format: "+ c);
    try {
        Number  d = numberFormatDutch.parse(c);
        BigDecimal bd = new BigDecimal(d.toString());
        System.out.println(bd);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

Currency Format: € 100.000,00

100000

like image 144
Gladiator Avatar answered Mar 24 '23 08:03

Gladiator