# rounding with printf function

Page 1 of 1

## 3 Replies - 6276 Views - Last Post: 11 April 2010 - 06:07 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=167464&amp;s=595541b97d2638a686b939b31b057afd&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 zacmac21

Reputation: 0
• Posts: 19
• Joined: 11-April 10

# rounding with printf function

Posted 11 April 2010 - 05:21 PM

I'm trying to get my program to round the average of the exam scores to 2 decimal places at the end of the code and can't get the printf function to work like I think it's supposed to. It's not rounding anything, just putting .00 at end of whatever number it comes up with. Any help would be appreciated. Here is the code.
``` import java.util.*;

import java.util.Scanner;   // Needed for the Scanner class

public class ExamScoreWebWorking
{
public static void main(String[]args)
{
Scanner in = new Scanner (System.in);  // create a Scanner object used to

int score = 0;
int count = 0;
int scorea=0, scoreb=0, scorec=0, scored=0, scoref=0;
double percenta, percentb, percentc, percentd, percentf;
int total;
double average;

Scanner keyboard = new Scanner(System.in);

total = 0;
count = 0;

System.out.print ("Enter final exam scores (-1 to end): ");
score = keyboard.nextInt();

while (score >= 0)
{
if (score >= 90)
{
++scorea;
total = total + score;
}
else if (score >= 80)
{
++scoreb;
total = total + score;
}
else if (score >= 70)
{
++scorec;
total = total + score;
}
else if (score >= 60)
{
++scored;
total = total + score;
}
else if (score <= 59)
{
++scoref;
total = total + score;
}

++count;

System.out.print ("Enter final exam scores (-1 to end): ");
score = keyboard.nextInt();
}

/*	 data validation
while (score > 100)
{ 	System.out.println ("Input error");
score = keyboard.nextInt();
}  */

System.out.println ("\nTotal number of scores: " + count);

System.out.println ("\tTotal number of A's:  " + scorea);
System.out.println ("\tTotal number of B's:  " + scoreb);
System.out.println ("\tTotal number of C's:  " + scorec);
System.out.println ("\tTotal number of D's:  " + scored);
System.out.println ("\tTotal number of F's:  " + scoref);

percenta = 100 * scorea / count;
percentb = 100 * scoreb / count;
percentc = 100 * scorec / count;
percentd = 100 * scored / count;
percentf = 100 * scoref / count;

System.out.println ("\tA: " + percenta +"%");
System.out.println ("\tB: " + percentb +"%");
System.out.println ("\tC: " + percentc +"%");
System.out.println ("\tD: " + percentd +"%");
System.out.println ("\tF: " + percentf +"%");

average = total/count;

System.out.printf ("\n The average score: %.2f\n", average);

}
}

```

Is This A Good Question/Topic? 0

## Replies To: rounding with printf function

### #2 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12278
• Posts: 45,364
• Joined: 27-December 08

## Re: rounding with printf function

Posted 11 April 2010 - 05:24 PM

Since total and count are both ints, the result of total/count will be an int. This means that any decimal places will be truncated before the result is assigned to average. So if you have 9/2, the result will be 4, which will then be cast to a double when assigned to average, so the end result is 4.0. If you want the result to be 4.5, you can cast either total or count to double during division, like so: average = (double)total/count;. The cast tells Java to read total's value as a double, so 9.0/2 = 4.5.

### #3 zacmac21

Reputation: 0
• Posts: 19
• Joined: 11-April 10

## Re: rounding with printf function

Posted 11 April 2010 - 06:07 PM

Thank you! I knew it had to be something like that... much appreciated.

### #4 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12278
• Posts: 45,364
• Joined: 27-December 08

## Re: rounding with printf function

Posted 11 April 2010 - 06:07 PM

Not a problem. Glad I could help!