io - Java - Rounding off 2 decimal places -
lets have user input. class standing has total of 80 , exam 100. how round result off 2 decimal places?
system.out.print("grades = 50% class standing + 50% exams = " + (cs/160+exam/200)*100);
you can use printf()
:
system.out.printf( "grades = 50%% class standing + 50%% exams = %.2f%n", (cs/160+exam/200)*100);
the %.2f
specifier indicates float/double should rounded 2 decimal places. %n
new line.
also note have escape literal %
s %
(i.e. %%
) not mistaken format specifiers.
Comments
Post a Comment