-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint formatting
110 lines (80 loc) · 3.56 KB
/
print formatting
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
Ref: https://docs.oracle.com/javase/tutorial/java/data/numberformat.html
integer format with string
*/
An Example
The following table lists some of the converters and flags that are used in the sample program, TestFormat.java, that follows the table.
Converters and Flags Used in TestFormat.java Converter Flag Explanation
d A decimal integer.
f A float.
n A new line character appropriate to the platform running the application. You should always use %n, rather than \n.
tB A date & time conversion—locale-specific full name of month.
td, te A date & time conversion—2-digit day of month. td has leading zeroes as needed, te does not.
ty, tY A date & time conversion—ty = 2-digit year, tY = 4-digit year.
tl A date & time conversion—hour in 12-hour clock.
tM A date & time conversion—minutes in 2 digits, with leading zeroes as necessary.
tp A date & time conversion—locale-specific am/pm (lower case).
tm A date & time conversion—months in 2 digits, with leading zeroes as necessary.
tD A date & time conversion—date as %tm%td%ty
08 Eight characters in width, with leading zeroes as necessary.
+ Includes sign, whether positive or negative.
, Includes locale-specific grouping characters.
- Left-justified..
.3 Three places after decimal point.
10.3 Ten characters in width, right justified, with three places after decimal point.
The following program shows some of the formatting that you can do with format. The output is shown within double quotes in the embedded comment:
import java.util.Calendar;
import java.util.Locale;
public class TestFormat {
public static void main(String[] args) {
long n = 461012;
System.out.format("%d%n", n); // --> "461012"
System.out.format("%08d%n", n); // --> "00461012"
System.out.format("%+8d%n", n); // --> " +461012"
System.out.format("%,8d%n", n); // --> " 461,012"
System.out.format("%+,8d%n%n", n); // --> "+461,012"
double pi = Math.PI;
System.out.format("%f%n", pi); // --> "3.141593"
System.out.format("%.3f%n", pi); // --> "3.142"
System.out.format("%10.3f%n", pi); // --> " 3.142"
System.out.format("%-10.3f%n", pi); // --> "3.142"
System.out.format(Locale.FRANCE,
"%-10.4f%n%n", pi); // --> "3,1416"
Calendar c = Calendar.getInstance();
System.out.format("%tB %te, %tY%n", c, c, c); // --> "May 29, 2006"
System.out.format("%tl:%tM %tp%n", c, c, c); // --> "2:34 am"
System.out.format("%tD%n", c); // --> "05/29/06"
}
}
System.out.printf("%-14s %03d %n", s1, x);
================================
java 100
cpp 065
python 050
================================
System.out.printf("%-14s %09d %n", s1, x);
================================
java 000000100
cpp 000000065
python 000000050
================================
System.out.printf("%-14s %15d %n", s1, x);
Your Output (stdout)
================================
java 100
cpp 65
python 50
================================
System.out.printf("%14s %03d %n", s1, x);
Your Output (stdout)
================================
java 100
cpp 065
python 050
================================
System.out.printf("%-14s %04d %n", s1, x);
================================
java 0100
cpp 0065
python 0050
================================