Feras Odeh
Feras Odeh

Reputation: 9296

NumberFormat parse method returns negative result!

I'm trying to format a String value to a valid currency double as follows

NumberFormat currency=NumberFormat.getCurrencyInstance();
currency.parse("10").doublevalue();

The problem is that I got back a negative number I got -10!. So is there any thing wrong with this code?

Thanks

Upvotes: 0

Views: 847

Answers (2)

laher
laher

Reputation: 9110

I think you're misunderstanding the role of NumberFormat a little bit.

You can simply call:

   double d= Double.parseDouble("10");
   NumberFormat currency=NumberFormat.getCurrencyInstance();
   System.out.println(currency.format(d));

For me, this prints out £10.00, and that's the string it would expect to be passed into the parse method

HTH

Upvotes: 2

amukhachov
amukhachov

Reputation: 5900

Try to use just

double d = Double.parseDouble("10");

Upvotes: 0

Related Questions