-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprecision.java
More file actions
57 lines (39 loc) · 1.5 KB
/
Copy pathprecision.java
File metadata and controls
57 lines (39 loc) · 1.5 KB
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
public class precision {
public static void main(String[] args) {
double price1 = 9.99;
double price2 = 100.02;
double price3 = -54.01;
double price4 = 9999.99;
// . decimal point
System.out.printf("%.1f\n",price1);
System.out.printf("%.2f\n",price2);
System.out.printf("%.3f\n",price3);
// + output aa plus if positive
System.out.printf("%+.2f\n",price2);
System.out.printf("%+.2f\n",price3);
// , comma grouping separator
System.out.printf("%,.2f\n",price4);
// ( negative numbers enclosed in ()
System.out.printf("%(.2f\n",price2);
System.out.printf("%(.2f\n",price3);
// 0 = zero padding
// number = right justified padding
// negative number = left justified padding
int id1 = 1;
int id2 = 23;
int id3 = 456;
int id4 = 7890;
System.out.printf("id: %04d\n", id1);
System.out.printf("id: %04d\n", id2);
System.out.printf("id: %04d\n", id3);
System.out.printf("id: %04d\n", id4);
System.out.printf("%4d\n", id1);
System.out.printf("%4d\n", id2);
System.out.printf("%4d\n", id3);
System.out.printf("%4d\n", id4);
System.out.printf("%-4d\n", id1);
System.out.printf("%-4d\n", id2);
System.out.printf("%-4d\n", id3);
System.out.printf("%-4d\n", id4);
}
}