I'm doing code for my class that require to output the area, circumference, and the number of digits in both the area and circumference for a circle. The output is not supposed to be rounding up or down, but just calculate all the way out, however my code is not doing this properly. This is my code:
circumference = 2 * pi * radius;
area = pi * Math.pow(radius, 2);
digitString1 = Double.toString(circumference);
digitString2 = Double.toString(area);
int count1 = 0;
for (int i = 0, len = digitString1.length(); i < len; i++) {
if (Character.isDigit(digitString1.charAt(i))) {
count1++;
}
}
int count2 = 0;
for (int i = 0, len = digitString2.length(); i < len; i++) {
if (Character.isDigit(digitString2.charAt(i))) {
count2++;
}
}
System.out.println("The Circumference is: " + circumference);
System.out.println("The Area is: " + area);
System.out.println("Total number of digits in the circumference is: " + count1);
System.out.println("Total number of digits in the area is: " + count2);
}
When I input 11 for example, I get:
Circles selected. Please enter the radius: 11
The Circumference is: 69.11503837897544
The Area is: 380.132711084365
Total number of digits in the circumference is: 16
Total number of digits in the area is: 15
Instead of the correct output:
Circles selected. Please enter the radius: 11
The circumference is: 69.11503837897544
The area is: 380.1327110843649
Total number of digits in the circumference is: 16
Total number of digits in the area is: 16
See how the ...649 at the end of the area rounded up to ...65. Can someone please help? Thanks!