Decimal Format

S

sanna

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....

DecimalFormat df = new DecimalFormat("##############0.00");
boolean appendO = false;
StringBuffer buff = new StringBuffer();
Double amt = new Double(df.format(amount));
String sAmount = amt.toString();
String a = sAmount.replace(',',' ');
StringTokenizer tok = new StringTokenizer(a,".");
while (tok.hasMoreTokens())
{
String token1 = tok.nextToken();
String token2 = tok.nextToken();
if (token2.length()<2)
{
appendO = true;
...................

Thanks,

Swiss
 
K

karlheinz klingbeil

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(',',' ');
StringTokenizer tok = new
StringTokenizer(a,"."); while
(tok.hasMoreTokens())
{
String token1 = tok.nextToken();
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,992
Messages
2,570,220
Members
46,807
Latest member
ryef

Latest Threads

Top