public class Demo {
public static void main(String args[])
{
double d = 12.345;
System.out.printf("|%7.3f|\n",d);
}
}
Output is |12.345|
what does 7.3f indicates.I would also like to know others options that can be used with printf.
If I use
System.out.printf("|%5.3f|\n",d);
I get the same output?why?
Both specifiers (7.3 and 5.3) say “use 3 digits of precision”. The difference between them is “min width 7” vs “min width 5”. The output of both is a 6 char long string of digits and the decimal point that has 3 digits of precision.
The width specification is useful when the actual width of the data is less-than or equal-to the specified width. If the data requires more than the specified width to be displayed, then the width is ignored.
PrintStream