Java Reference
In-Depth Information
EXAMPLE 3-4
//Example: Fixed and scientific format
public class ScientificVsFixed
{
public static void main(String[] args)
{
double hours = 35.45;
double rate = 15.00;
double tolerance = 0.01000;
System.out.println("Fixed decimal notation:");
System.out.printf("hours = %.2f, rate = %.2f, pay = %.2f,"
+ " tolerance = %.2f%n%n",
hours, rate, hours * rate, tolerance);
System.out.println("Scientific notation:");
System.out.printf("hours = %e, rate = %e, pay = %e,%n"
+ "tolerance = %e%n",
hours, rate, hours * rate, tolerance);
}
}
Sample Run:
Fixed decimal notation:
hours = 35.45, rate = 15.00, pay = 531.75, tolerance = 0.01
Scientific notation:
hours = 3.545000e+01, rate = 1.500000e+01, pay = 5.317500e+02,
tolerance = 1.000000e-02
The sample run shows how the value of hours , rate , pay , and tolerance are printed
in fixed decimal format and in scientific notation. First the values are printed in fixed
decimal format using the conversion f and then the values are printed in scientific
notation using the conversion e .
EXAMPLE 3-5
//Program to illustrate how to format the outputting of
//decimal numbers.
public class FormattingDecimalNumNew
//Line 1
{
//Line 2
static final double PI = 3.14159265;
//Line 3
public static void main(String[] args)
//Line 4
{
//Line 5
double radius = 12.67;
//Line 6
double height = 12.00;
//Line 7
 
Search WWH ::




Custom Search