-4

I’m new in Java. I want use printf to print a double number. When I use println, it work correctly. But use either System.out.printf("%.2d\n" + s.avrage); or System.out.printf("%.2f\n" + s.avrage); a java.util.IllegalFormatPrecisionException occurred.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Kaaveh Mohamedi
  • 1,399
  • 1
  • 15
  • 36

2 Answers2

0

It should be something like this

    float f = 12.0f;
    System.out.printf("%.2f", f);
Khantahr
  • 8,156
  • 4
  • 37
  • 60
SparkOn
  • 8,806
  • 4
  • 29
  • 34
0

The problem with your attempt,

System.out.printf("%.2f\n" + s.avrage);

Is you are using a + which results in String concatenation, instead of passing the second (expected) argument to printf. You wanted something like,

System.out.printf("%.2f\n", s.avrage); // <-- comma, not plus
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249