0

I want to convert Indian Currency into double. Below is my code:

String initalAmount = "2341";
Format format = NumberFormat.getCurrencyInstance(new Locale("en", "in"));
String convertedAmountToINR = format.format(new BigDecimal(initalAmount));
// it prints Rs. 2,341.00

Now, I want to convert it into DOUBLE like 2341.00. I tried couple of ways to get it worked but didn't.

halfer
  • 19,824
  • 17
  • 99
  • 186
VVB
  • 7,363
  • 7
  • 49
  • 83
  • Why don't you store the new BigDecimal value and use that? This looks like homework to me. – Warren P Jan 12 '16 at 12:38
  • You can convert it to a double with `Double d = Double.parseDouble(initalAmount);` but I don't think that's what you mean. – ONOZ Jan 12 '16 at 12:40
  • [Dont use `double` for currency](https://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency). – Raedwald Sep 20 '19 at 09:28

3 Answers3

2

Please try like this,

double doubleINR=Double.parseDouble(convertedAmountToINR);

For your case try following,

  double d=Double.parseDouble(convertedAmountToINR.replaceAll("Rs.|,", ""));
Bharat DEVre
  • 539
  • 3
  • 13
  • Great answer. It worked for me. Better if you provide a little description for used regex. – VVB Jan 12 '16 at 14:14
0

You should convert initialAmount to a double like this:

Double.valueOf(initalAmount);

The reason why you can't convert convertedAmountToINR is because the "Rs." in it can't be converted into a double.

Sync
  • 3,571
  • 23
  • 30
0

If you really want to, you can convert the convertedAmountToINR back with the same format object:

NumberFormat format = NumberFormat.getCurrencyInstance(new Locale("en", "in"));
double value = format.parse(convertedAmountToINR).doubleValue();

This will only parse strings that were formatted with this format object.

Daniel Fekete
  • 4,988
  • 3
  • 23
  • 23