Advertisement
cotolonco

StringFormatExamplev1.0

Jul 1st, 2014
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.07 KB | None | 0 0
  1. public class StringFormat
  2. {
  3.   public static void main(String[] args)
  4.   {
  5.     String first = "first";
  6.     int dato1 = 400;
  7.     double dato2 = 0.567;
  8.     String second = "second";
  9.     int dato3 = 1;
  10.     double dato4 = 300.5123456789;
  11.    
  12.     //A la derecha los numeros
  13.    
  14.     String format1 = String.format("%-15s%15d%15.4f", first, dato1, dato2);
  15.     String format2 = String.format("%-15s%15d%15.4f", second, dato3, dato4);
  16.    
  17.     System.out.println(format1);
  18.     System.out.println(format2);
  19.    
  20.     //A la izquierda los numeros
  21.    
  22.     System.out.println();
  23.    
  24.     format1 = String.format("%-15s%-15d%-15.4f", first, dato1, dato2);
  25.     format2 = String.format("%-15s%-15d%-15.4f", second, dato3, dato4);
  26.    
  27.     System.out.println(format1);
  28.     System.out.println(format2);
  29.    
  30.     /* Output:
  31.      *
  32.      * first                      400         0,5670
  33.      * second                       1       300,5123
  34.      *
  35.      * first          400            0,5670          
  36.      * second         1              300,5123  
  37.      *
  38.      * */
  39.    
  40.   }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement