double to String problem


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: double to String problem

Hybrid View

  1. #1
    Max Fedorov Guest

    double to String problem


    I designed a reporting engine that calculats totals at the end, so I needed
    to convert double to String, in order to append it to a StringBuffer variable.

    String text = new String().valueOf(double);

    or

    StringBuffer strBuf = new StringBuffer();
    strBuf.append(double);
    cell.addObject(new HTMLText(strBuf.toString()));

    But after the convertion the value 6.20, for instance, becomes
    6.199999999999998

    Do you have any suggestions?
    Thanx.

  2. #2
    Jim Deutch Guest

    Re: double to String problem

    There's lots of different ways to do it, but I've rounded Doubles to 4
    significant figures with

    private final double r4 = 10000d;
    Double startx = new Double((double)Math.round(startPoint.x*r4)/r4);

    Jim Deutch

    Max Fedorov wrote in message <38db9904$1@news.devx.com>...
    >
    >I designed a reporting engine that calculats totals at the end, so I needed
    >to convert double to String, in order to append it to a StringBuffer

    variable.
    >
    >String text = new String().valueOf(double);
    >
    >or
    >
    >StringBuffer strBuf = new StringBuffer();
    >strBuf.append(double);
    >cell.addObject(new HTMLText(strBuf.toString()));
    >
    >But after the convertion the value 6.20, for instance, becomes
    >6.199999999999998
    >
    >Do you have any suggestions?
    >Thanx.




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center