0

I have an assignment that lets people enter a shape and side lengths/the radius and gives an area and circumference/perimeter. The issue I am having however is out of my depth.

area = (Math.PI) * (radius) * (radius);
circumference = 2 * Math.PI * radius;
System.out.printf("The circumference is: %f\n", circumference);
System.out.printf("The area is: %f\n", area);
String areaString = String.valueOf(area);
String perimeterString = String.valueOf(circumference);
System.out.println("Total number of digits in the circumference is: " + areaString.length());
System.out.println("Total number of digits in the area is: " + perimeterString.length());

This is the code. The radius was entered before. If it makes any difference the area is a double and the value inputed by the user is an integer. So the output of the code is:

The circumference is: 69.115038
The area is: 380.132711
Total number of digits in the circumference is: 17
Total number of digits in the area is: 17

So what I believe is happening is the double's length is actually more than is being printed out (obviously pi is irrational). However, when I change the first print statement to:

System.out.println("Total number of digits in the circumference is: " + (areaString.length()-1));

eclipse still prints out 17. If there is anyone out there more experienced I would appreciate some advise.

1 Answers1

3

The conclusion seems inescapable that %f does a different conversion than String.valueOf.

But stepping back: this whole goal is really, deeply not going to work. Measuring the "number of decimal digits" in a double is not a thing you can do. If you add 0.1 and 0.2, you get 0.30000000000000004, see here. That's unavoidable in the way doubles are stored. So the number of "decimal digits" is really going to be meaningless.

Community
  • 1
  • 1
Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
  • How do you think I should go about this? I need to be able to get the number of digits for the assignment and this method is how some classmates told me they went about this. – Conrad Melcher Oct 11 '16 at 22:30
  • 1
    To clarify why `%f` behaves differently, the [javadoc](https://docs.oracle.com/javase/8/docs/api/java/util/Formatter.html#dndec) say: *If the precision is not specified then the default value is 6.* That is why both values are printed with 6 digits after the decimal point, regardless of the actual precision of the value. – Andreas Oct 11 '16 at 22:37
  • @ConradMelcher it would be a very strange assignment that asks for the number of digits in a `double` value. What is the exact wording? – Klitos Kyriacou Oct 11 '16 at 22:45
  • It's quite possible that it's just a bad assignment. – Louis Wasserman Oct 12 '16 at 00:03
  • Everything was figured out, instead of using the printf I just wen println and used a + in order to add the number at the end. I'm not sure why I was supposed to get the length of the number, but now the program is working properly and the values I'm getting for both the area and the perimeter is exactly what the instructor wants. Thanks for everyone's help, I really appreciate it – Conrad Melcher Oct 12 '16 at 00:48