본문 바로가기
Spring/Java

[ Java ] String.format() - 문자열 형식 설정

by snow_hong 2022. 4. 8.

String.format() - 문자열 형식을 설정하는 메서드, Java 5 이상부터 사용 가능

fomat() 서식은 크게 7가지로 나뉜다.

  1. %d (10진수 형식)
  2. %s (문자열 형식)
  3. %f (실수형 형식)
  4. %t(날짜시간 형식)
  5. %c (유니코드 문자 형식)
  6. %o , %x (8진수, 16진수 형식)
  7. Locale 설정

 

1. %d (10진수 형식) - integer 형식

//구분을 위해서 맨 마지막에 _포함
int i = 127;

//기본
System.out.println(String.format("%d_", i));    //127_

//길이설정
System.out.println(String.format("%5d_", i));   //  127_

//길이설정+왼쪽정렬
System.out.println(String.format("%-5d_", i));  //127  _

//길이보다 작을경우 0을 붙임
System.out.println(String.format("%05d_", i));  //00127_


int a = 123456789;

System.out.println(String.format("%,d_", a));     //123,456,789_
System.out.println(String.format("%,15d_", a));   //    123,456,789_
System.out.println(String.format("%,-15d_", a));  //123,456,789    _
System.out.println(String.format("%,015d_", a));  //0000123,456,789_
  • %d : 기본적으로 오른쪽 정렬이다.  왼쪽 정렬을 할 경우에는 -를 붙인다. ex) -%d
  • %숫자d : 글자 길이 설정 ex) %5d
  • %0d : 표현한 숫자가 길이가 작을 경우 앞에 0을 붙인다. (0이 아닌 숫자를 넣으면 글자 길이로 인식, integer형식이다 보니 문자는 안된다.)

 

2. %s (문자열 형식) - String 형식

 String str = "javaHong";

//기본
System.out.println(String.format("%s_", str));        //javaHong_
System.out.println(String.format("%2s_", str));        //javaHong_

//대문자
System.out.println(String.format("%S_", str));       //JAVAHONG_

//오른쪽에 공백추가
System.out.println(String.format("%12s_", str));      //    javaHong_

//왼쪽에 공백추가
System.out.println(String.format("%-12s_", str));     //javaHong    _

//최대 n길이만큼 출력
System.out.println(String.format("%.2s_", str));      //ja_

//
System.out.println(String.format("%-12.3s_", str));   //jav         _
System.out.println(String.format("%12.4s_", str));    //        java_
  • %s : 문자열 출력
  • %S : 대문자로 문자열 출력
  • %숫자s : 문자열이 숫자보다 클 경우 공백을 추가해서 보여줌
  • %-숫자s : 왼쪽 정렬 (기본은 오른쪽 정렬)
  • %.숫자s : 숫자 길이만큼 문자열이 출력된다.

 

3. %f (실수형 형식) - floating point 형식 (부동 소수점)

double n = 123.456789;

System.out.println(12.3);                          //12.3
System.out.println(String.format("%f_", 12.3));    //12.300000_
        
System.out.println(n);                             //123.456789
System.out.println(String.format("%f_", n));       //123.456789_
System.out.println(String.format("%.7f_", n));     //123.4567890_
System.out.println(String.format("%15f_", n));     //     123.456789_
System.out.println(String.format("%-15f_", n));    //123.456789     _
System.out.println(String.format("%.3f_", n));     //123.457_
System.out.println(String.format("%.2f_", n));     //123.46_
System.out.println(String.format("%15.2f_", n));   //         123.46_
System.out.println(String.format("%-15.2f_", n));  //123.46         _
System.out.println(String.format("%10.5f_", n));   // 123.45679_
System.out.println(String.format("%015f_", n));    //00000123.456789_
System.out.println(String.format("%015.2f_", n));  //000000000123.46_
  • %f : %.6f와 같은 의미 (소수점 아래 6자리) / 소수점 아래는 반올림하여 출력
  • %숫자.숫자f : 앞에 숫자는 글자 길이를 의미, 뒤에 숫자는 소수점 아래 숫자만큼 표현 (. 도 글자 길이에 포함)
  • %0숫자f : 0을 붙이면 왼쪽 공백에 0으로 채워준다.

 

4. %t (날짜 시간 형식) - DateTime 형식

Date now = new Date();

//기본적인 날짜 포맷
System.out.println(now);  //Fri Apr 08 03:07:13 CEST 2022
System.out.println(String.format("%%tF(yyyy-MM-dd): %tF", now));  //%tF(yyyy-MM-dd): 2022-04-08
System.out.println(String.format("%%tT(02H:02m:02s): %tT, %%tR(02H:02m): %tR", now, now));  //%tT(02H:02m:02s): 03:07:13, %tR(02H:02m): 03:07
System.out.println(String.format("%%ty(2y): %ty, %%tY(4y): %tY", now, now));	//%ty(2y): 22, %tY(4y): 2022	
System.out.println(String.format("%%tm(02M): %tm", now));		//%tm(02M): 04
System.out.println(String.format("%%td(02d): %td, %%te(d): %te", now, now));  //%td(02d): 08, %te(d): 8
System.out.println(String.format("%%tH(02H): %tH", now));  //%tH(02H): 03
System.out.println(String.format("%%tM(02m): %tM", now));  //%tM(02m): 07
System.out.println(String.format("%%tS(02s): %tS", now)); //%tS(02s): 13
System.out.println(String.format("%%tZ(time zone): %tZ, %%tz(time zone offset): %tz", now, now));  //%tZ(time zone): CEST, %tz(time zone offset): +0200


//더 다양한 날짜 포맷
System.out.println(String.format("%%tA(day of Week, Full name): %tA, %%ta: %ta", now, now));  //%tA(day of Week, Full name): Friday, %ta: Fri
//%tB와 %tb는 한글로는 똑같이 출력, 영어만 차이남
System.out.println(String.format("%%tB(month, Full name): %tB, %%tb: %tb", now, now));  //%tB(month, Full name): April, %tb: Apr
System.out.println(String.format(Locale.ENGLISH, "%%tB(month, Full name): %tB, %%tb: %tb", now, now));  //%tB(month, Full name): April, %tb: Apr
System.out.println(String.format("%%tc(= %%ta %%tb %%td %%tT %%tZ %%tY): %tc", now));  //%tc(= %ta %tb %td %tT %tZ %tY): Fri Apr 08 03:30:08 CEST 2022
System.out.println(String.format("%%tD(MM/dd/yy): %tD", now));  //%tD(MM/dd/yy): 04/08/22
System.out.println(String.format("%%td(02d): %td, %%te(d): %te", now, now));  //%td(02d): 08, %te(d): 8
System.out.println(String.format("%%tF(yyyy-02M-02d): %tF", now));  //%tF(yyyy-02M-02d): 2022-04-08
System.out.println(String.format("%%tH(02H, 00-23): %tH, %%tk(H, 0-23): %tk", now, now)); //%tH(02H, 00-23): 03, %tk(H, 0-23): 3
System.out.println(String.format("%%tI(02h, 01-12): %tI, %%tl(h, 1-12): %tl", now, now));  //%tI(02h, 01-12): 03, %tl(h, 1-12): 3
System.out.println(String.format("%%tj(day of Year, 001-366): %tj", now));  //%tj(day of Year, 001-366): 098
System.out.println(String.format("%%tp(오전 또는 오후): %tp", now));  //%tp(오전 또는 오후): am
  • 기본적으로 시간 및 날짜 형식에는 leading-0s를 붙인다.
  • %tF : 연-월-일
  • %tT : 시:분:초
  • %ty : 년도(2자리) ex) 22
  • %tY : 년도(4자리) ex)2022
  • %tm : 월
  • %td : 0 포함 일 ex)08
  • %te : 0 제거 일 ex) 8
  • %tH : 시, 24-hour
  • %th : 시, 12-hour
  • %tM : 분
  • %tS : 초

 

5. %c (유니코드 문자 형식) - Unicode char 형식 

System.out.println(String.format("48 → %c, 57 → %c", 48, 57));    //48 → 0, 57 → 9
System.out.println(String.format("65 → %c, 90 → %c", 65, 90));    //65 → A, 90 → Z
System.out.println(String.format("97 → %c, 122 → %c", 97, 122));  //97 → a, 122 → z
System.out.println(String.format("44032 → %c, 55203 → %c", 44032, 55203));  //44032 → 가, 55203 → 힣
  • %c : 숫자를 유니코드로 변환한다.

 

6. %o , %x (8진수, 16진수 형식)

int n = 100;

System.out.println(String.format("10진수(%d) : 2진수(%s)", n, Integer.toBinaryString(n)));//10진수(100) : 2진수(1100100)
System.out.println(String.format("8진수(%o), 16진수(%x)", n, n)); //8진수(144), 16진수(64)

 

7. Locale 설정

int money = 35000;
Date today = new Date();

System.out.println(String.format("₩ %,d", money));  //₩ 35,000

//유로화는 3자리 구분자를 .을 이용
System.out.println(String.format(Locale.GERMANY, "%,d €", money)); //35.000 €

//%tp는 Local.ENGLISH에서는 am,pm을 표현함
System.out.println(String.format("%tp", today));  //오전

System.out.println(String.format(Locale.ENGLISH, "%tp", today)); //am
  • String.format(포맷, 값);
  • String.format(Locale, 포맷, 값); : 국가별 포맷 설정 가능 (Locale을 설정을 안 하면 OS에 설정되어있는 값을 default로 적용)
728x90

댓글