+ 1

Question about double

public class Program { public static void main(String[] args) { double a = 12.3; double b = 12.0; double c = a-b; System.out.println(c); //output: 0.300000000000007 double d = Math.rint(c*10) / 10; System.out.println(d); //output: 0.3 } } Why I have to use Math.rint to get 0.3? Has it do to with the type double or with Java?

1st Jan 2019, 11:21 PM
Denise Roßberg
Denise Roßberg - avatar
3 odpowiedzi
+ 1
Not necessarily, usually double precision is used for large numbers only
2nd Jan 2019, 4:14 PM
Da2
Da2 - avatar
0
Thank you for the fast answer. So I should every time use this math method when I use double?
1st Jan 2019, 11:29 PM
Denise Roßberg
Denise Roßberg - avatar
0
But in my case I don't have large. The code above belongs to my program: https://code.sololearn.com/c5C9tWQq8Qlm/?ref=app It did'nt worked without using Math.rint ().
2nd Jan 2019, 11:11 PM
Denise Roßberg
Denise Roßberg - avatar