Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2006

Tip: Looking for answers? Try searching our database.

Decimal Format

Thread view: 
sanna - 07 Jul 2006 14:13 GMT
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
karlheinz klingbeil - 07 Jul 2006 14:42 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.