jdalbey

FormatNumber

Jan 16th, 2014
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.92 KB | None | 0 0
  1.  
  2. import java.text.DecimalFormat;
  3. import java.text.NumberFormat;
  4. /**
  5. Modify this program to produce right-justified columns, like this:
  6.  
  7.  23   5 213   9 325
  8.   4 123  23  35   3
  9. 879  21   8 223  98
  10.  
  11. */
  12.  
  13. public class FormatNumber
  14. {
  15.     public static void main(String[] args)
  16.     {
  17.         int[][] numbers = { {23, 5, 213, 9, 325},
  18.                             {4, 123, 23, 35, 3},
  19.                             {879, 21, 8, 223, 98}
  20.                           };
  21.                          
  22.         NumberFormat formatter;
  23.         String pattern = "###";
  24.         for (int row = 0; row < 3; row++)
  25.         {
  26.             for (int col = 0; col < 5; col++)
  27.             {
  28.                 formatter = new DecimalFormat(pattern);
  29.                 String number = formatter.format(numbers[row][col]);
  30.                 System.out.print(number + " ");
  31.             }
  32.             System.out.println();
  33.         }
  34.     }
  35.    
  36. }
Advertisement
Add Comment
Please, Sign In to add comment