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.
Asked
Active
Viewed 74 times
-4

Luiggi Mendoza
- 85,076
- 16
- 154
- 332

Kaaveh Mohamedi
- 1,399
- 1
- 15
- 36
-
3Pass the `double` as a separate argument and use `f`. ā rgettman Aug 14 '14 at 19:00
-
possible duplicate of [how do I use System.out.printf?](http://stackoverflow.com/questions/18730978/how-do-i-use-system-out-printf) ā Mansueli Aug 14 '14 at 19:02
-
Change to `System.out.printf("%.2f\n" , s.avrage)` ā Jens Aug 14 '14 at 19:03
2 Answers
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