June 2017
Beginner
1296 pages
69h 23m
English
... "%+d\t%+d\n", 786, -786);
10 } // end main
11 } // end class PlusFlagTest
786 -786 +786 -786
Figure I.17 prefixes a space to the positive number with the space flag. This is useful for aligning positive and negative numbers with the same number of digits. The value -547 is not preceded by a space in the output because of its minus sign. Figure I.18 uses the # flag to prefix 0 to the octal value and 0x to the hexadecimal value.
1 // Fig. I.17: SpaceFlagTest.java
2 // Printing a space before non-negative values.
3
4 public class SpaceFlagTest
5 {
6 public static void main(String[] args)
7 {
8 System.out.printf("% d\n% d\n", 547, -547);
9 } // end main
10 } // end class SpaceFlagTest ...Read now
Unlock full access