Java Reference
In-Depth Information
DecimalFormat twoDecimal =
new DecimalFormat("0.00");
//Line 4
DecimalFormat threeDecimal =
new DecimalFormat("0.000");
//Line 5
System.out.println("Line 6: Outputting the "
+ "values of x, y, and z \n"
+ " with two decimal "
+ "places.");
//Line 6
System.out.println("Line 7: x = "
+ twoDecimal.format(x));
//Line 7
System.out.println("Line 8: y = "
+ twoDecimal.format(y));
//Line 8
System.out.println("Line 9: z = "
+ twoDecimal.format(z));
//Line 9
System.out.println("Line 10: Outputting the "
+ "values of x, y, and z \n"
+ " with three "
+ "decimal places.");
//Line 10
System.out.println("Line 11: x = "
+ threeDecimal.format(x));
//Line 11
System.out.println("Line 12: y = "
+ threeDecimal.format(y));
//Line 12
System.out.println("Line 13: z = "
+ threeDecimal.format(z));
//Line 13
}
}
Sample Run:
Line 6: Outputting the values of x, y, and z
with two decimal places.
Line 7: x = 15.67
Line 8: y = 235.73
Line 9: z = 9525.99
Line 10: Outputting the values of x, y, and z
with three decimal places.
Line 11: x = 15.674
Line 12: y = 235.730
Line 13: z = 9525.986
The statements in Lines 1, 2, and 3 declare and initialize x , y , and z to 15.674 , 235.73 ,
and 9525.9864 , respectively. The statement in Line 4 creates and initializes the
DecimalFormat object twoDecimal to output decimal numbers to two decimal places.
Similarly, the statement in Line 5 creates and initializes the DecimalFormat object
threeDecimal to output decimal numbers with three decimal places.
The statements in Lines 7, 8, and 9 output the values of x , y , and z to two decimal places,
respectively. Note that the printed values of x in Line 7 and z in Line 9 are rounded.
Search WWH ::




Custom Search