my homework exercise gave skeleton of attendaces and my job was to name weeks so it maches values in arrays and get avarage attendance for each week and get output and the code is:
public class AttendanceTest {
public static void main(String[] args) {
final Integer CLASS_SIZE = 15;
Integer[] attendances = {10, 14, 12, 9, 11, 15, 12, 13, 8, 14};
final Integer ACADEMIC_WEEKS = 10;
final String[] weeks = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
Double totalAvg;
for (Integer week = 0; week < ACADEMIC_WEEKS; week++) {
totalAvg = (double)(attendances[week]*100)/CLASS_SIZE;
System.out.println(weeks[week] + " " +totalAvg);
}
}
}
and gives output of:
1 66.66666666666667 2 93.33333333333333 3 80.0 4 60.0 5 73.33333333333333 6 100.0 7 80.0 8 86.66666666666667 9 53.333333333333336 10 93.33333333333333
how can i round the output to 2 decimal point using String.format and/or .places?
so the output will be:
1 66.67 2 93.33 3 80.00 4 60.00 5 73.33 6 100.00 7 80.00 8 86.67 9 53.33 10 93.33