Java Reference
In-Depth Information
System.out.println("Two decimal places: ");
//Line 8
System.out.printf("Line 9: radius = %.2f, "
+ "height = %.2f, volume = %.2f, "
+ "PI = %.2f%n%n", radius, height,
PI * radius * radius * height, PI);
//Line 9
System.out.println("Three decimal places: ");
//Line 10
3
System.out.printf("Line 11: radius = %.3f, "
+ "height = %.3f, volume = %.3f,%n"
+ " PI = %.3f%n%n", radius,
height,PI * radius * radius * height, PI);
//Line 11
System.out.println("Four decimal places: ");
//Line 12
System.out.printf("Line 13: radius = %.4f, "
+ "height = %.4f, volume = %.4f,%n "
+ " PI = %.4f%n%n", radius,
height,PI * radius * radius * height, PI);
//Line 13
System.out.printf("Line 14: radius = %.3f, "
+ "height = %.2f, PI = %.5f%n",
radius, height, PI);
//Line 14
}
//Line 15
}
//Line 16
Sample Run:
Two decimal places:
Line 9: radius = 12.67, height = 12.00, volume = 6051.80, PI = 3.14
Three decimal places:
Line 11: radius = 12.670, height = 12.000, volume = 6051.797,
PI = 3.142
Four decimal places:
Line 13: radius = 12.6700, height = 12.0000, volume = 6051.7969,
PI = 3.1416
Line 14: radius = 12.670, height = 12.00, PI = 3.14159
In this program, the statement in Line 9 outputs the values of
radius
,
height
, the
volume, and
PI
to two decimal places. The statement in Line 11 outputs the values of
radius
,
height
, the volume, and
PI
to three decimal places. The statement in Line 13
outputs the values of
radius
,
height
, the volume, and
PI
to four decimal places. The
statement in Line 14 outputs the value of
radius
to three decimal places, the value of
height
to two decimal places, and the value of
PI
to five decimal places.
Notice how the values of
radius
are printed at Lines 11, 13, and 14. The value of
radius
printed in Line 11 contains a trailing
0
. This is because the stored value of
radius
has only two decimal places, a
0
is printed at the third decimal place. In a similar
manner, the value of
height
is printed in Lines 11, 13, and 14.
Search WWH ::
Custom Search