[Java] printf 출력에서 %d와 %2d의 차이

irever1029·2022년 5월 5일
0

기타

목록 보기
11/12

예제를 통해 배워보자.

public class Ex_JS {

	public static void main(String[] args) {
		int a = 7;
		int b = -25;
		int c = 200;
		
		System.out.printf("%7d%n", a);
		System.out.printf("%07d%n", b);
		System.out.printf("%+7d%n", c);
        System.out.printf("%-7d%n", c);
	}
}

출력값:
      7
-000025
   +200
200    

%와 d사이에 오는 숫자만큼 자리를 확보한다.

+는 숫자가 양수일때 숫자앞에 +를 표시하고
-는 숫자를 왼쪽으로 정렬한다.

출처: https://en.wikipedia.org/wiki/Printf_format_string

profile

0개의 댓글