# number of decimal places in a double

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

1. ### harryosGuest

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

2. ### markspaceGuest

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

3. ### Joshua CranmerGuest

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
4. ### Roedy GreenGuest

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
5. ### LewGuest

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