Rounding error with double


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: Rounding error with double

  1. #1
    Join Date
    Jun 2004
    Posts
    29

    Rounding error with double

    I've got a problem with multiplying a double by 100.0. Soemtimes it works, other times it gives me rounding problems. Specifically, when the original value was 80000.01, multiplying it by 100.0 (d = 80000.01; d *= 100.0 gave me 80000.9999999 instead of 80001.0. How can I prevent this?

    Gib

  2. #2
    Join Date
    Dec 2004
    Location
    france
    Posts
    35
    Well here's what i thing :
    you should just think about multiplying it all by 100 and then dividing it by 100;
    you sould get it right then

  3. #3
    Join Date
    Jun 2004
    Posts
    29
    Tried that, didn't work.

  4. #4
    Join Date
    Dec 2004
    Location
    france
    Posts
    35
    Allright check this out :
    import java.util.*;
    class round{
    public static void main(String[] lalama){
    double a=80000.01;
    double b=100.0;
    double c=a*b;

    System.out.println(Math.floor(c+1));
    }}

  5. #5
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    I think this method is ok if you operate w.in the range of a long. There is however 'simpler' methods available i the BigDecimal class, but I haven't got the documentation/libraries for that.

    PS: I haven't given the negative aspect much thought...

    import java.util.*;
    public class Rounder {

    public Rounder() {}
    public void doA(double aDouble, double mult) {
    double b=100.0;
    double c=aDouble*mult;
    System.out.println(Math.floor(c+1)+ " dammm");
    }
    public void doB(double aDouble, double mult, int precision) {
    double c=aDouble*mult;
    double factor=Math.pow(10,precision);
    c=(double) ((long)(c*factor)/factor);
    System.out.println(c);
    }

    public static void main(String[] lalama){
    Rounder r=new Rounder();
    r.doA(80000.0105d, 100.0d);
    r.doB(90000.0105d,100.0d,2);
    }
    }
    eschew obfuscation

  6. #6
    Join Date
    Nov 2004
    Posts
    13
    This is a very easy way that works.

    double d = 80000.01;
    d = Math.round(d * 100);

    This will work if you know your answer is going to be an integer value.

    other options:

    // rounds down to nearest integer
    d = Math.floor(d*100 +.5);

    // casts as an integer (rounds down)
    d = (int)d * 100;

  7. #7
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Originally posted by rogueace
    This is a very easy way that works.

    double d = 80000.01;
    d = Math.round(d * 100);

    This will work if you know your answer is going to be an integer value.

    other options:

    // rounds down to nearest integer
    d = Math.floor(d*100 +.5);

    // casts as an integer (rounds down)
    d = (int)d * 100;
    OK, but its not the integer range that is the probklem, its numbers that don'tdisplay properly due to their internal binary representation. As it is there are several numbers that cannot be represented binary w. the required precision.

    I recommend a chekout of the BigDecimal utilities to put end end to the problem, regardless of magnitude.
    eschew obfuscation

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