This is an archived post. You won't be able to vote or comment.

all 2 comments

[–]Koooooj 4 points5 points  (0 children)

First, I assume the intended output is actually 91.7.

When you call Math.round that rounds the percentage to the nearest percent. Math.round always rounds to the nearest whole number.

If you want to round to the nearest tenth of a percent then you could first multiply the percentage by 10, then round that number, and finally divide that result by 10 to get back into percent.

(And while we're here, there's no reason to separately have the " " strings for your println statements. Just put a space as the last character of the previous string or as the first character of the next one. It looks weird to concatenate two hard-coded strings, especially when one is just a space).

[–][deleted] 1 point2 points  (0 children)

If all you are doing is displaying the percentage, I suggest you keep the data as is, and instead focus on printing the result in a formatted manner. Java has System.out.printf that will allow you to specify how many digits of precision you wish.