Thread: Double precison 131.76/24.0 = 5.4899 when it shoudl be 5.49

1. Member
Join Date
Jun 2009
Posts
2
Rep Power
0

Double precison 131.76/24.0 = 5.4899 when it shoudl be 5.49

Sorry if this is the wrong place.

Here is my question. We are trying to do some Double double division on 131.76 and 24.0. The result in the console is 5.4899 when it should be 5.49. Anyone have any explanation for this?

Here is some sample code...

Double a = 131.76;
Double b = 24.0;
System.out.println(a/b);

Is the precision on Doubles that bad? The math seems to work 99% of the time. Its this and a few other special occasions where it craps out....

2. Hi,
Just check this format code.It will help u.I have not executed.Please try to execute this with different combination of numbers.

Java Code:
```import java.text.*;
class Test
{
public static void main(String[] args)
{
Double a = 131.76;
Double b = 24.0;
Double output = a/b;
System.out.println("Testing decimal places rounding!  "+ new DecimalFormat().format(output));
}
}```

3. Member
Join Date
Jun 2009
Posts
2
Rep Power
0
Originally Posted by RamyaSivakanth
Hi,
Just check this format code.It will help u.I have not executed.Please try to execute this with different combination of numbers.

Java Code:
```import java.text.*;
class Test
{
public static void main(String[] args)
{
Double a = 131.76;
Double b = 24.0;
Double output = a/b;
System.out.println("Testing decimal places rounding!  "+ new DecimalFormat().format(output));
}
}```
In my particular case it is not about formatting the decimal, that can easily be done. What am curious about is the underlying Double division problem. Is this a known flaw? Am I doing something wrong and getting the wrong response? Would the use of BigDecimal solve the issue?

4. Hi,
I think there is no problem.It is maintaing the accuracy like this.For precision sake u can go ahead with BigDecimal.

-Regards
Ramya

5. Last edited by DarrylBurke; 06-25-2009 at 11:03 PM.

6. Senior Member
Join Date
Aug 2008
Posts
384
Rep Power
6
It's simply said the way Doubles (and floats) work. For an exact number you should use BigDecimal, as Ramya said.