Results 1 to 6 of 6
  1. #1
    2ssjb is offline Member
    Join Date
    Jun 2009
    Posts
    2
    Rep Power
    0

    Default 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...

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    835
    Rep Power
    6

    Default

    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:

  3. #3
    2ssjb is offline Member
    Join Date
    Jun 2009
    Posts
    2
    Rep Power
    0

    Default

    Quote Originally Posted by RamyaSivakanth View Post
    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. #4
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    835
    Rep Power
    6

    Default

    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
    Ramya:cool:

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default

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

  6. #6
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    It's simply said the way Doubles (and floats) work. For an exact number you should use BigDecimal, as Ramya said.
    I die a little on the inside...
    Every time I get shot.

Similar Threads

  1. Replies: 5
    Last Post: 03-21-2009, 09:32 PM
  2. Double
    By Xystus777 in forum New To Java
    Replies: 2
    Last Post: 01-21-2009, 10:39 AM
  3. Double.valueOf() vs Double.parseDouble()
    By greenbean in forum New To Java
    Replies: 10
    Last Post: 01-12-2009, 08:39 AM
  4. Using double for money is a sin
    By fishtoprecords in forum New To Java
    Replies: 3
    Last Post: 11-28-2008, 07:05 AM
  5. Float vs. Double
    By javanub in forum New To Java
    Replies: 1
    Last Post: 11-23-2008, 12:11 PM

Tags for this Thread

Posting Permissions

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