Right justifying integers in fields. - Java Language Basics

Java examples for Language Basics:Number Format

Description

Right justifying integers in fields.

Demo Code

public class Main 
{
   public static void main(String[] args)
   { /* w ww  . ja  va  2 s.com*/
      System.out.printf("%4d\n", 1);
      System.out.printf("%4d\n", 12);
      System.out.printf("%4d\n", 123);
      System.out.printf("%4d\n", 1234);
      System.out.printf("%4d\n\n", 12345); // data too large 

      System.out.printf("%4d\n", -1);
      System.out.printf("%4d\n", -12);
      System.out.printf("%4d\n", -123);
      System.out.printf("%4d\n", -1234); // data too large 
      System.out.printf("%4d\n", -12345); // data too large 
   } 
}

Result


Related Tutorials