Natasha Reaney wrote:Working on an assignment and my code is automatically rounding to one decimal place, even though I've assigned double to the variables. Anyone know why?
Not exactly, because we don't know what values you put in. However, I suspect very strongly that it's not rounding anything at all; it's simply displaying your doubles in their "default" form, which always has at least one digit after the decimal point.
My suggestion: back up and explain what you want - exactly - not what you're getting.
@ Campbell Ritchie - Unfortunately, as this is an assignment for a course, I am limited to using what we have learned in the course to date. We haven't touched on hasNextDouble methods.
@ Jeff Verdegan - Not sure I know where to add print statements. The output is correct, that is, I am getting the results I expect, only there is just one zero after the decimals, not multiple zeros.
@ Winston Gutkowski - Not sure I know what you mean by displaying my doubles in their "default" form, is the default always just one digit after the decimal?
My input data is:
10 10 10 100 5
5 5 5 25 10
Natasha Reaney wrote:
The output is correct, that is, I am getting the results I expect, only there is just one zero after the decimals, not multiple zeros.
Okay, so, as others have suggested, this is purely a problem of formatting. When you call Sytem.out.println(some_double), somewhere down the line that double has to get turned into a String, which means a decision has to be made how to format it. Since you didn't specify any particular formatting, a default format is used. Apparently that format specifies a single zero after the decimal if there's no fractional part.
If you want a different formatting, you can use java.text.DecimalFormat or System.out.printf() or String.format().
It is an experimental device that will make my mind that most powerful force on earth! More powerful than this tiny ad!
The WEB SERVICES and JAX-RS Coursehttps://coderanch.com/t/690789/WEB-SERVICES-JAX-RS