-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path24_printf
More file actions
42 lines (35 loc) · 1.92 KB
/
24_printf
File metadata and controls
42 lines (35 loc) · 1.92 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
// printf() = an optional method to control, format, and display text to the console window
// two arguments = format string + (object/variable/value)
// % [flags] [precision] [width] [conversion-character]
public class Main {
public static void main(String[] args){
System.out.printf("%d This is a format string",123); //>>123 This is a format string
boolean myBoolean = true;
char myChar = '@';
String myString = "Bro";
int myInt = 50;
double myDouble = 1000;
//[conversion-character]
System.out.printf("%b",myBoolean);
System.out.printf("%c",myChar);
System.out.printf("%s",myString);
System.out.printf("%d",myInt);
System.out.printf("%f",myDouble);
//[width]
// minimum number of characters to be written as output
System.out.printf("Hello %10s", myString); // >>Hello Bro|
System.out.printf("Hello %-10s", myString);// >>Hello Bro |
//[precision]
// sets number of digits of precision when outputting floating-point values
System.out.printf("You have this much money %.1f", myDouble);// >>1d.p.
//[flags]
// add an effect to output based on the flag added to format specifier
// - : left-justify
// + : output a plus (+) or minus (-) sign for a numeric value
// 0 : numeric values are zero-padded
// , : comma grouping separator if numbers > 1000
System.out.printf("You have this much money %20f", myDouble); // >>You have this much money 1000.000000
System.out.printf("You have this much money %+f", myDouble);// >>You have this much money +1000.000000
System.out.printf("You have this much money %020f", myDouble);// >>You have this much money -000000001000.000000
System.out.printf("You have this much money %,f", myDouble); // >>You have this much money 1,000.000000
}