Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Decimal Format

Reply
Thread Tools

Decimal Format

 
 
sanna
Guest
Posts: n/a
 
      07-07-2006

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

 
Reply With Quote
 
 
 
 
karlheinz klingbeil
Guest
Posts: n/a
 
      07-07-2006
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
TypeError: unsupported operand type(s) for -: 'Decimal' and 'Decimal'. Why? Gilbert Fine Python 8 08-01-2007 01:58 AM
converting exponential format number to decimal format number Fei Liu Perl Misc 21 12-16-2006 01:49 AM
Decimal to Packed Decimal Conversion in C++ Ven C++ 3 08-01-2006 03:56 PM
Money Format + Decimal Place Format shannon Java 1 02-02-2006 03:47 PM
Money Format + Decimal Place Format shannon Java 0 02-01-2006 10:02 PM



Advertisments