Results 1 to 6 of 6
- 06-25-2009, 03:53 PM #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....
Thanks for your help in advance...
- 06-25-2009, 04:32 PM #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)); } }Ramya:cool:
- 06-25-2009, 04:41 PM #3
Member
- Join Date
- Jun 2009
- Posts
- 2
- Rep Power
- 0
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 #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
RamyaRamya:cool:
- 06-25-2009, 09:59 PM #5
Last edited by DarrylBurke; 06-25-2009 at 10:03 PM.
- 06-26-2009, 09:09 AM #6
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 5
Similar Threads
-
non-static method add(double,double) cannot be referenced from a static context
By cravi85 in forum Java SoftwareReplies: 5Last Post: 03-21-2009, 09:32 PM -
Double
By Xystus777 in forum New To JavaReplies: 2Last Post: 01-21-2009, 10:39 AM -
Double.valueOf() vs Double.parseDouble()
By greenbean in forum New To JavaReplies: 10Last Post: 01-12-2009, 08:39 AM -
Using double for money is a sin
By fishtoprecords in forum New To JavaReplies: 3Last Post: 11-28-2008, 07:05 AM -
Float vs. Double
By javanub in forum New To JavaReplies: 1Last Post: 11-23-2008, 12:11 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks