public class Solution {
public static void main(String[] args) {
printCircleCircumference(5);
}
public static void printCircleCircumference(int radius) {
System.out.println(2*3.14*radius);
}
}
Why is the output 31.400000000000002? I would expect 31.4Why so many decimal places? 31.400000000000002
Resolved
Comments (4)
- Popular
- New
- Old
You must be signed in to leave a comment
P S
28 April 2020, 15:37
Wait I don't get it
0
John W Lolo
27 April 2020, 18:37
Thank you Tiym, that makes a lot of sense!
To better understand your explanation, I went into excel and divided a decimal number by 3 and it ended up 15 decimal places: .666666666666667, which appears to behave functionally like Java's 15 digit representation in binary of .000000000000002. Fantastic explanation, worded exactly in a way I would understand it. Thanks again.
0
Gellert Varga
27 April 2020, 23:32
And if you don't want to display so many zeros (0.000000000002), then you can format your decimal number:
https://www.tutorialspoint.com/java_i18n/java_i18n_decimalformat.htm
0
Artem Divertitto Senior Android Developer at United Tech
27 April 2020, 07:31
This kind of mistake connected with internal(binary) representation of numbers. Like that at decimal number system you can't representation result of divisions 1/3, that at binary number system you can't represent 1/10
0