Durga
Durga

Reputation: 565

how to convert currency format to double in java

I am getting value in currency formate, but i want double format only.

String amt = txn.getAmount();
System.out.println("--amt--"+amt);//output:1010
double value = Double.parseDouble(amt);
System.out.println("---value---"+value);//output:1010.0
String ammount=NumberFormat.getCurrencyInstance().format(value);
System.out.println("--ammount--"+ammount);//output:Rs.1,010.00

Here i want Rs.1,010.00 to 1010.00

Any mistakes in my code?

Upvotes: 0

Views: 4436

Answers (3)

dev8080
dev8080

Reputation: 4020

I assume you do not want the currency details. In that case, use getNumberInstance() instead of getCurrencyInstance().

Use:

NumberFormat nf = NumberFormat.getNumberInstance();
nf.setGroupingUsed(false);
nf.setMinimumFractionDigits(2);
String ammount= nf.format(value);

Upvotes: 3

diyoda_
diyoda_

Reputation: 5420

Try this cleaner approach!.

      double d = 1010.00;
      Locale uk = new Locale("en", "GB");
      NumberFormat cf = NumberFormat.getCurrencyInstance(uk);
      String s = cf.format(d);

      System.out.println(s);

      Number number = null;
      try
      {
         number = cf.parse(s);
      }
      catch (ParseException e)
      {
         System.out.print(e);
      }
      double dClone = number.doubleValue();

Upvotes: 0

Ria Sen
Ria Sen

Reputation: 94

Before printing, replace the string "Rs." with "" and also "," with "".

String replaceString1=amount.replace("Rs.","");
String replaceString2=amount.replace(",","");

This is a way to handle this case. Hope this helps.

Upvotes: 0

Related Questions