System.out.format method format multiple arguments based on format string.
Basic example
public class Main { public static void main(String[] args) { int int1 = 10; double double1 = 10.2; String str1 = "Hello"; System.out.format("%d %f %s\n", int1, double1, str1); } }
10 10.200000 Hello
print integer 8 characters in width
public class Main2 { public static void main(String[] args) { int int1 = 245; System.out.format("%8d\n",int1); // Min 8 character width. Pad with space if needed System.out.format("%08d\n",int1); // Min 8 character width. Pad with 0 if needed } }
245 00000245
print float with 2 digits after decimal point
public class Main3 { public static void main(String[] args) { double double1 = 10.2456; System.out.format("%.2f\n",double1); } }
10.25