Ch2-12~13. printf()
0. 목차
Chapter2. 변수
Ch2 - 12. printf를 이용한 출력
Ch2 - 13. printf를 이용한 출력 예제
Ch2 - 12. printf를 이용한 출력
▶ println()의 단점 - 출력형식 지정불가
▷ 실수의 자리수 조정불가 - 소수점 n자리만 출력하려면?
· 정수 ÷ 정수 = 정수
System.out.println(10/3);
// console
3
· 실수 ÷ 정수 = 실수
System.out.println(10.0/3);
// console
3.33333333...
▷ 10진수로만 출력 - 8진수, 16진수로 출력하려면?
System.out.println(0x1A);
// console
26
▶ printf()로 출력형식 지정가능
▷ “%.2f” : 소수점 둘째자리까지 출력
System.out.printf("%.2f", 10.0/3);
// console
3.33
▷ “%d” : 10진수로 출력
System.out.printf("%d", 0x1A);
// console
26
▷ “%x” : 16진수로 출력
System.out.printf("%x", 0x1A);
// console
1a
▶ printf()의 지시자 1
▷ 개행문자 : %n과 /n
%n은 OS 상관 없이 됨
System.out.printf("age:%d year:%d%n줄바꿈ok?", 14, 2017);
// console
age:14 year:2017
줄바꿈ok?
/n은 OS별로 조금 다를 수 있음
System.out.printf("age:%d year:%d\n줄바꿈ok?", 14, 2017);
// console
age:14 year:2017
줄바꿈ok?
▶ printf()의 지시자 2
▷ 정수 ‘15’를 10진수
로 출력
System.out.printf("%d", 15);
// console
15
▷ 정수 ‘15’를 8진수
로 출력
System.out.printf("%o", 15);
// console
17 // 일칠 10진수에서 1은 8을 의미
▷ 정수 ‘15’를 16진수
로 출력
System.out.printf("%x", 15);
// console
f
▷ toBinaryString : 정수를 2진 문자열로 변환
System.out.printf("%s", Integer.toBinaryString(15));
// console
1111
▷ 8진수와 16진수에 접두사 붙이기
#을 붙여주기
System.out.printf("%#o%n", 15);
System.out.printf("%#x%n", 15);
System.out.printf("%#X%n", 15); // X를 대문자로 사용하면 접두사도 대문자로 출력
// console
017
0xf
0XF
▷ 실수 출력 지시자 %f
+ 지수형식 %e
float f = 123.4567890f;
System.out.printf("%f%n", f);
System.out.printf("%e%n", f);
// console
123.456787 // f는 정밀도 7자리 123.4567 = 총 7자리 그 외 87 두 자리는 의미 없는 수
1.234568e+02 // 8e+02 = ① 8은 반올림 ② e+02는 10² → 지수형식
+ 간략한 형식 %g
System.out.printf("%g%n", 123.456789);
System.out.printf("%g%n", 0.00000001);
// console
123.457 // 간단한 형식 - 7자리 내로 표현 %f
1.00000e-08 // 간단한 형식 - 지수 형식 %e
▶ printf()의 지시자 3
▷ 지정한 자리수만큼 출력 : %5d(지정한 자리수는 5자리)
System.out.printf("[%5d]", 10);
// console
[ 10] // 출력 수 = 10(두 자리) / 지정 수 = 5자리 → 앞 3자리는 비움
▷ 왼쪽정렬 : %-d
System.out.printf("[%-5d]", 10);
// console
[10 ]
▷ 공백 채우기 : %05d(5자리에서 공백인 자리는 0으로 채움)
System.out.printf("[%05d]", 10);
// console
[00010]
▷ %전체자리 수.소수점아래자리 수f
double d = 123.456789;
System.out.printf("d=%14.10f", d);
// console
d=123.4567890000 // '.'포함 하여 총 14자리, 소수점 아래는 총 10자리, 빈자리는 0으로 채움
▷ 문자열 출력 : %s
String url = "100jran.github.io";
System.out.printf("%s", url);
// console
100jran.github.io
▷ 지정한 자리수만큼 출력 : %20s(지정한 자리수는 20자리)
String url = "100jran.github.io";
System.out.printf("[%20s]", url);
// console
[ 100jran.github.io]
▷ 왼쪽 정렬 : %-20s
String url = "100jran.github.io";
System.out.printf("[%-20s]", url);
// console
[100jran.github.io ]
▷ 부분 출력 : %.8s(전체 중 8자리만 출력)
String url = "100jran.github.io";
System.out.printf("[%.8s]", url);
// console
[100jran.] // '.'포함 총 8자리
Ch2 - 3. printf를 이용한 출력 예제
▶ 정수 출력
▷ 10진수 ‘10’ 출력
System.out.printf("%d", 10);
// console
10
▷ 10진수 ‘10’을 8진수로 출력
System.out.printf("%o", 10);
// console
12
▷ 10진수 ‘10’을 16진수로 출력
System.out.printf("%x", 10);
// console
a
▷ 10진수 ‘10’을 10진수, 8진수, 16진수로 접두사 붙여 출력
System.out.printf("%d%n", 10);
System.out.printf("%#o%n", 10);
System.out.printf("%#x%n", 10);
// console
10
012
0xa
▷ 10진수 ‘10’을 2진수 문자열로 출력
System.out.printf("%s", Integer.toBinaryString(10));
// console
1010
▶ 실수 출력
▷ float 타입으로 ‘123.456789’ 출력
float f = 123.456789f;
System.out.printf("%f", f);
// console
123.456787 // float 타입은 끝 두자리 수 87 = 의미없는 수(정밀도가 7자리까지 7자리 이후 수는 의미無)
▷ double 타입으로 ‘123.456789’ 출력
double f = 123.456789;
System.out.printf("%f", f);
// console
123.456789 // double 타입 끝 두자리 89 = 의미있는 수(정밀도가 15자리까지 15자리까지의 수는 의미有)
▷ double 타입의 ‘123.456789’를 %e로 출력
double f = 123.456789;
System.out.printf("%e", f);
// console
1.234568e+02
▷ double 타입의 ‘123.456789’를 %g로 출력
double f = 123.456789;
System.out.printf("%g", f);
// console
123.457
▶ 지정 출력
▷ 지정자리 10자리 → ‘10’ 출력
System.out.printf("[%10d]", 10);
// console
[ 10]
지정자리 수가 출력할 값 보다 크다면?
지정자리 수 무시 → 값 출력
System.out.printf("[%2d]", 12345);
// console
[12345]
▷ 지정자리 10자리 + 왼쪽 정렬로 → ‘10’ 출력
System.out.printf("[%-10d]", 10);
// console
[10 ]
▷ 지정자리 10자리 + 공백은 0으로 채워 → ‘10’ 출력
System.out.printf("[%010d]", 10);
// console
[0000000010]
▷ 전체 16자리 + 소수점 아래는 10자리로 → ‘1.23456789’ 출력
System.out.printf("[%16.10f]", d);
// console
[ 1.2345678900]
▷ 지정자리 20자리 + 왼쪽정렬 → ‘100jran.github.io’ 출력
String url = "100jran.github.io";
System.out.printf("[%20s]", url);
// console
[100jran.github.io ]
▷ ‘100jran.github.io’ 출력 → 10자리만 출력
System.out.printf("[%.10s]", url);
// console
[100jran.gi]