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

• 06-25-2009, 03:53 PM
2ssjb
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....

• 06-25-2009, 04:32 PM
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.

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));         } }```
• 06-25-2009, 04:41 PM
2ssjb
Quote:

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.

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?
• 06-25-2009, 05:03 PM
RamyaSivakanth
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
• 06-25-2009, 09:59 PM
DarrylBurke
• 06-26-2009, 09:09 AM
Supamagier
It's simply said the way Doubles (and floats) work. For an exact number you should use BigDecimal, as Ramya said.