-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrintfMethod.java
More file actions
63 lines (49 loc) · 1.63 KB
/
PrintfMethod.java
File metadata and controls
63 lines (49 loc) · 1.63 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
58
59
60
61
62
63
public class PrintfMethod {
public static void main(String[] args){
//printf() = an optional mehtod to control,format, and display text to the console window
// Conversion Characters
boolean myBoolean = true;
System.out.printf("AAAAA %b AAAAA",myBoolean);
System.out.println();
int myInteger = 1;
System.out.printf("AAAA %d AAAA",myInteger);
System.out.println();
char myChar = 'w';
System.out.printf("AAAA %c AAAA",myChar);
System.out.println();
String myString = "Weditha";
System.out.printf("AAAA %s AAAA",myString);
System.out.println();
float myFloat = 0.2f;
System.out.printf("AAAA %f AAAA",myFloat);
System.out.println();
Double myDouble = 343434.334344;
System.out.printf("AAAA %f AAAA",myDouble);
System.out.println();
//-------------------------------------------------------------------
// Width
String name = "Bro";
System.out.printf("My name is %s *",name); //Normal
System.out.println();
System.out.printf("My name is %10s*",name); //align to the right side
System.out.println();
System.out.printf("My name is %-10s*",name); //align to the left side
System.out.println();
//Precision
double x = 1.23456;
System.out.printf("%f",x); //Normal
System.out.println();
System.out.printf("%.2f",x); //Rounded
System.out.println();
//Flags
int y = 100000;
System.out.printf("%,d",y);
System.out.println();
System.out.printf("%010d",y);
System.out.println();
System.out.printf("-%d",y);
System.out.println();
System.out.printf("+%d",y);
System.out.println();
}
}