DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

# Thread: Rounding error with double

1. Registered User
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. Registered User
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. Registered User
Join Date
Jun 2004
Posts
29
Tried that, didn't work.

4. Registered User
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. 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;
System.out.println(Math.floor(c+1)+ " dammm");
}
public void doB(double aDouble, double mult, int precision) {
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);
}
}

6. Registered User
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. 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.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 FAQ Latest Articles Java .NET XML Database Enterprise