Java examples for Language Basics:Number Format
Right justifying integers in fields.
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 } }