Source of TestNumberFormatting.java


  1: //TestNumberFormatting.java
  2: 
  3: public class TestNumberFormatting
  4: {
  5:     public static void main(String args[])
  6:     {
  7:         //More ways to format numbers
  8:         int m = 123;
  9:         int n = 4;
 10:         System.out.format("%5d", m);
 11:         System.out.println();
 12:         System.out.format("%2d", m);
 13:         System.out.println();
 14:         System.out.format("%5d", n);
 15:         System.out.println();
 16:         System.out.format("Two values: %5d and %5d", m, n);
 17:         System.out.println();
 18: 
 19:         double x = 3.14159;
 20:         double y = -123.45;
 21:         System.out.format("%10.3f", x);
 22:         System.out.println();
 23:         System.out.format("%10.6f", x);
 24:         System.out.println();
 25:         System.out.format("%5.4f", x);
 26:         System.out.println();
 27:         System.out.format("Two values: %5.2f and %5.2f", x, y);
 28:         System.out.println();
 29:     }
 30: }
 31: