Mega Code Archive

 
Categories / Java Tutorial / Development
 

Specifying the Width and Precision

public class MainClass {   public static void main(String[] args) {     double x = 27.5, y = 33.75;        System.out.printf("x = %15f y = %8g", x, y);   } } x = 27.500000 y = 33.7500 public class MainClass {   public static void main(String[] args) {     int a = 5, b = 15, c = 255;        System.out.printf("a = %1$5d b = %2$5x c = %3$2o", a, b, c);        } } a = 5 b = f c = 377 public class MainClass {   public static void main(String[] args) {     int a = 5, b = 15, c = 255;        System.out.printf("%na = %1$-5d b = %2$-5x c = %3$-5o", a, b, c);   } } a = 5 b = f c = 377 public class MainClass {   public static void main(String[] args) {     double x = 27.5, y = 33.75;        System.out.printf("x = %15.2f y = %14.3g", x, y);   } } x = 27.50 y = 33.8