previous | start | next

Formatted Printing: printf

    1   
    2   public class PrintfTester
    3   {
    4     public static void main(String[] args)
    5     {
    6        double x, y, z, sum;
    7        x = Math.random() * 100;
    8        y = Math.random() * 100;
    9        z = Math.random() * 100;
   10   
   11        sum = x + y + z;
   12   
   13        System.out.printf("%10s %6.2f\n", "first", x);
   14        System.out.printf("%10s %6.2f\n", "second", y);
   15        System.out.printf("%10s %6.2f\n", "third", z);
   16        System.out.printf("%10s ------\n", "");
   17        System.out.printf("%10s %6.2f\n", "sum", sum);
   18     }
   19   }

Output:

     first  93.60
    second   3.13
     third  49.83
           ------
       sum 146.56
     


previous | start | next