String.format() 이란?

String의 static 메서드인 format 메서드는 문자열의 형식을 설정하는 메서드다.

%d (= Integer Formatting)

10진수 integer의 형식을 설정할 때 이용한다.

int num = 19;

System.out.println(String.format("%d", num)); // 19

% 바로 뒤에 , 를 붙이면 3자리 단위로 쉼표를 찍어준다.

int num = 1000;

System.out.println(String.format("%,d", num)); // 1000

&s (= String Formatting)

문자열의 형식을 설정할 때 이용한다.

String str = "test";

System.out.println(String.format("%s", str));

%f (= Floating point Formatting)

실수형 숫자 형식을 설정할 때 이용한다.

double n = 123.4567;

System.out.println(String.format("%f", n));
System.out.println(String.format("%.2f", n)); // 123.46