number of decimal places in a double

Discussion in 'Java' started by harryos, Nov 17, 2009.

  1. harryos

    harryos Guest

    hi
    I am trying this calculation

    double a=2.2;
    double b=1.8;
    double sum=a+b;
    double diff=a-b;
    double remainder=a%b;

    when I print the values of these, I get
    a=2.2
    b=1.8
    sum=4.0
    diff=0.40000000000000013
    remainder=0.3999999999999988

    In my calculations, I want to use diff as 0.4 instead of
    40000000000000013 .Also I would like to get remainder as 0.4 instead
    of 0.3999999999999988.
    Is there some way to do this using any java api?
    thanks
    harry.
     
    harryos, Nov 17, 2009
    #1
    1. Advertising

  2. harryos

    markspace Guest

    harryos wrote:
    > hi
    > I am trying this calculation
    >
    > double a=2.2;
    > double b=1.8;
    > double sum=a+b;
    > double diff=a-b;
    > double remainder=a%b;
    >
    > when I print the values of these, I get


    1) print with printf, and specify a precision

    System.out.printf( "%.6f", diff );

    I think will do it.

    2) Use BigDecimal.
     
    markspace, Nov 17, 2009
    #2
    1. Advertising

  3. On 11/17/2009 09:52 AM, harryos wrote:
    > hi
    > I am trying this calculation
    >
    > double a=2.2;
    > double b=1.8;
    > double sum=a+b;
    > double diff=a-b;
    > double remainder=a%b;
    >
    > when I print the values of these, I get
    > a=2.2
    > b=1.8
    > sum=4.0
    > diff=0.40000000000000013
    > remainder=0.3999999999999988
    >
    > In my calculations, I want to use diff as 0.4 instead of
    > 40000000000000013 .Also I would like to get remainder as 0.4 instead
    > of 0.3999999999999988.
    > Is there some way to do this using any java api?


    There is no way to get 0.4 as a floating-point number since the number
    2/5 has an infinite expansion in binary. There is a number closest to
    that value in the floating point representation, and the Java
    double-to-String conversion routines magically detect that and print out
    the simple numbers (see
    <http://java.sun.com/javase/6/docs/api/java/lang/Double.html#toString(double)>).

    For most floating point comparisons, you need to consider answers to
    within an epsilon (10^-6 is generally good enough for many applications;
    see the field of numerical analysis for much more information). For
    printing out, you can generally limit the number of significant figures
    to a few (6 is again generally good enough).

    --
    Beware of bugs in the above code; I have only proved it correct, not
    tried it. -- Donald E. Knuth
     
    Joshua Cranmer, Nov 17, 2009
    #3
  4. harryos

    Roedy Green Guest

    On Tue, 17 Nov 2009 06:52:53 -0800 (PST), harryos
    <> wrote, quoted or indirectly quoted someone
    who said :

    >Is there some way to do this using any java api?


    See http://mindprod.com/jgloss/floatingpoint.html
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    Without deviation from the norm, progress is not possible.
    ~ Frank Zappa (born: 1940-12-21 died: 1993-12-04 at age: 52)
     
    Roedy Green, Nov 17, 2009
    #4
  5. harryos

    Lew Guest

    harryos wrote, quoted or indirectly quoted someone who said :
    >> Is there some way to do this using any java api?

    >


    Roedy Green wrote:
    > See <http://mindprod.com/jgloss/floatingpoint.html>
    >


    Also read "What Every Computer Scientist Should Know About Floating-
    Point Arithmetic",
    <http://docs.sun.com/source/806-3568/ncg_goldberg.html>
    Complete PDF:
    <http://dlc.sun.com/pdf/800-7895/800-7895.pdf>

    --
    Lew
     
    Lew, Nov 17, 2009
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    14
    Views:
    64,962
    rkjessop
    Jul 6, 2012
  2. Gizmo
    Replies:
    1
    Views:
    3,627
    Jakob Bieling
    Aug 31, 2003
  3. shez

    number of decimal places

    shez, Jan 20, 2005, in forum: C++
    Replies:
    4
    Views:
    6,794
    saurabh.unercat
    Feb 19, 2011
  4. Gaijinco
    Replies:
    29
    Views:
    2,973
    Anonymous 7843
    Sep 28, 2005
  5. Gaijinco
    Replies:
    27
    Views:
    1,107
    Anonymous 7843
    Sep 28, 2005
Loading...

Share This Page