This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Code automatically rounding  RSS feed

 
Natasha Reaney
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

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?



The result shows up like this:
BEGIN
10.0:10.0
10.0:110.0
20.0:110.0
20.0:10.0...

Thank you!!
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest adding a bunch of println() statemetns so that you can see what your code is actually doing, compare that to what you think it should be doing, and use that result to figure out where your incorrect assumptions lie.
 
Campbell Ritchie
Marshal
Posts: 55715
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also you should not use a Scanner and parseDouble() together. You should use the nextDouble and hasNextDouble methods. Combine them to make a method in a utility class.Search my posts in this forum and “Java in General” for “utility class” and you will (I hope) get several hits. Put them together and you can make your utility class. You can get a double with MyUtilities.doubleFromKeyboard() or similar.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Winston
 
Natasha Reaney
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the responses!

@ 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
END
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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().
 
Natasha Reaney
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, thank you!!! That makes so much sense now. I made the change and it worked! I really appreciate the help guys!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!