0

I'm doing this for a class assignment. Please keep in mind that I'm learning and new at this. I've compiled my code and it runs fine. It's doing what i want.

Part of the class assignment is that when it prints that unnecessary decimals don't show up when not needed e.g. 85.00. Here is my code.

public class Assignment1    {
    public static void main (String cyclops[])  {

    Scanner input = new Scanner (System.in);
    System.out.print("Welcome to the CST8215 Final Mark Calculator \n");

    System.out.print("Entered Lab mark out of 10:  ");
    float lab = input.nextFloat();
    System.out.print("Entered Quiz/Test mark out of 10: ");
    float quiz_test = input.nextFloat();
    System.out.print("Entered assignment mark out of 20  ");
    float assignment = input.nextFloat();
    System.out.print("Entered midterm mark out of 20:  ");
    float midterm = input.nextFloat(); 
    System.out.print("Entered final exam mark out of 40  ");
    float final_exam = input.nextFloat();

     float theoryGrade = (quiz_test + midterm + final_exam) *100/70;

    float practicalGrade = (assignment + lab) *100/30;


    float finalGrade = (lab + quiz_test + assignment + midterm + final_exam);

    System.out.format("theoryGrade: %.2f%%%n",theoryGrade);

    System.out.format("practicalGrade: %.2f%%%n",practicalGrade);

    System.out.format("finalGrade: %.2f%%%n",finalGrade);
    } // end of main
}// end of class
ross
  • 1
  • 2
  • So if I understand correctly is the desired output: `85.00 -> 85`, `85.10 -> 85.1`, `85.01 -> 85.01` –  May 30 '18 at 07:57
  • Have a look at [DecimalFormat](https://docs.oracle.com/javase/8/docs/api/java/text/DecimalFormat.html) – Ben May 30 '18 at 07:57
  • Refer to this question: https://stackoverflow.com/questions/50532/how-do-i-format-a-number-in-java – Toza May 30 '18 at 07:58
  • yes when i run it it calculates the grades as theoryGrade: 74.29% practicalGrade: 85.00% finalGrade: 77.50%. some basically if its 74.00 and not 74.29 how do i get it to drop the .00 – ross May 30 '18 at 07:58

1 Answers1

0

Use new DecimalFormat("#.##") to limit to two decimal after comma

Tryliom
  • 111
  • 1
  • 17