sanna schrub am Freitag, 7. Juli 2006 15:13 folgendes:
> I am trying to convert a double value to two decimal
> places, but when I enter an amount say
> 12345678.99000, I get the result as 12345678.99E7
> instead of 12345678.99. Here is what my code looks
> like....
Of course you get 12345678.99E7 !
> DecimalFormat df = new
> DecimalFormat("##############0.00");
> boolean appendO = false;
> StringBuffer buff = new StringBuffer();
> Double amt = new Double(df.format(amount));
DecimalFormat df.format returns a String !
What you are doing is converting the String
("12345678.99") back into a double ("12345678.99E7")
and then again into a String (="12345678.99E7")
> String sAmount = amt.toString();
> String a = sAmount.replace(',',' ');
[quoted text clipped - 5 lines]
> String token2 = tok.nextToken();
> if (token2.length()<2)
I really don't know what this is supposed to be ???
Maybe converting Decimal Separators, which df.format
already returns according to your locale ? This is not
necessary, of course.
So your Code should look like:
DecimalFormat df = new DecimalFormat("##########0.00");
String sAmount = df.format(amount)
This is it, no Black Magic involved...
greetz Karlheinz Klingbeil (lunqual)
http://www.lunqual.de http://www.42pixels.de
http://www.rezeptbuch-pro.de