Java/기초
변수 값 출력하기
notcherry
2023. 7. 3. 18:32
728x90
반응형
정수 | %d | 정수 | 123 |
%6d | 6자리 정수, 왼쪽 빈 자리 공백 | ___123 | |
%-6d | 6자리 정수, 오른쪽 빈 자리 공백 | 123___ | |
%06d | 6자리 정수, 왼쪽 0 채움 | 000123 |
실수 | %10.2f | 소수점 이상 7, 이하 2자리, 왼쪽 공백 | ____123.45 |
%-10.2f | 소수점 이상 7, 이하 2자리, 오른쪽 공백 | 123.45____ | |
%010.2f | 소수점 이상 7, 이하 2자리, 왼쪽 0채움 | 0000123.45 |
문자열 -> %s로 원리 같음!
예제
public static void main(String[] args){
int value = 123;
System.out.printf("%d\n", value);
System.out.printf("%6d\n", value);
System.out.printf("%-6d\n", value);
System.out.printf("%+6d\n", value);
double area = 3.14159 * 10 * 10;
System.out.printf("반지름의 길이가 %d인 원의 넓이 : %10.2f\n", 10, area);
String name = "홍길동";
String job = "도적";
System.out.printf("%6d|%-10s|%10s\n", 1, name, job);
}
결과
println()은 리터럴 출력이라 printf("\n") 사용!
728x90
반응형