본문 바로가기

old/Programming

서식 문자 format string (%) 사용법

반응형

%d

데이터 형식: 10진수 byte, short, int, long

사용예제:

String temp = String.format("%d", 29);
System.out.println(temp); // 29

%숫자d

데이터 형식: 10진수 byte, short, int, long

사용예제:

String temp = String.format("%5d", 29);
System.out.println(temp); // _ _ _29

%o

데이터 형식: 8진수 byte, short, int, long

사용예제:

String temp = String.format("%o", 10);
System.out.println(temp); // 12

%x

데이터 형식: 16진수 byte, short, int, long

사용예제:

String temp = String.format("%x", 10);
System.out.println(temp); // a

%f

데이터 형식: 실수 float, double

사용예제:

String temp = String.format("%f", 123.4567);
System.out.println(temp); // 123.456700

%.숫자f

데이터 형식: 실수 float, double

사용예제:

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

%e

데이터 형식: e 표기법에 의한 실수

사용예제:

String temp = String.format("%e", 874.9163);
System.out.println(temp); // 8.749163e+02

%s

데이터 형식: 문자열(String) 데이터

사용예제:

String temp = String.format("%s", System.lineSeparator());
System.out.println(temp); // 줄바꿈

%c

데이터 형식: 문자(Character) 데이터

사용예제:

String temp = String.format("%c", 'y');
System.out.println(temp); // y
반응형