• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Code automatically rounding

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 76887
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Well don't expect me to do the dishes! This ad has been cleaned for your convenience:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic