Print Page Print Page

CH3 – Formatting Output

  1. Create a new file named FormatOutput.java in nano and add the following code.
public class FormatOutput{
  final static double CM_PER_INCH = 2.54;
  public static void main(String[] args){
    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);
  }
}
  1. Save, compile, and run the program.
$ javac FormatOutput.java
$ java FormatOutput
Four thirds = 1.333100 in = 254.000000 cm