forked from ChrisMayfield/ThinkJavaCode2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormatting.java
More file actions
33 lines (22 loc) · 777 Bytes
/
Formatting.java
File metadata and controls
33 lines (22 loc) · 777 Bytes
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
/**
* Examples from the middle of the chapter.
*/
public class Formatting {
public static void main(String[] args) {
final double CM_PER_INCH = 2.54;
System.out.println(System.out);
// formatting output
System.out.print(4.0 / 3.0);
System.out.printf("Four thirds = %.3f", 4.0 / 3.0);
int inch = 100;
double cm = inch * CM_PER_INCH;
System.out.printf("%d in = %f cm\n", inch, cm);
// type cast operators
double pi = 3.14159;
int xi = (int) pi;
double x = (int) pi * 20.0; // result is 60.0, not 62.0
inch = (int) (cm / CM_PER_INCH);
System.out.printf("%f cm = %d in\n", cm, inch);
System.out.printf("inches = %d" + inch); // error
}
}