코딩 개발/Java

String.format (%d(10진수), %s(문자열), %f(실수형 숫자))

호소세 2023. 10. 18. 16:32
728x90
반응형

시간, 월, 일, 번호 등등 을 표현할 때 단위 수를 맞추기 위해 앞에 0을 넣은 적이 있을 것입니다.

예를 들어, 09월 08일 / 00시 04분 같은 것이 있습니다.

뭐 간단하게 +를 사용하여 문자열에 붙여주면 되는데 숫자를 문자열로 바꾸고 더해줘야 합니다.

이러한 것을 조금 더 쉽게 변경할 수 있는 함수가 있어 알아보려고 합니다.

 

1.  %d (10진수 integer )

int i = 23;

System.out.println(String.format("%d❤️", i));
System.out.println(String.format("%5d_", i));
System.out.println(String.format("%-5d_", i));
System.out.println(String.format("%05d_", i));

결과

23❤️
   23_
23   _
00023_

1. String.format("%d❤️", i)

%d 뒤에 넣고 싶은 문자를 넣으면 마지막에 자신이 설정한 문자가 들어가게 됩니다.

하트, 언더 바를 넣었을 때 글자 맨 뒤에 붙게 됩니다.

 

2. String.format("%5d_", i)

% 뒤에 숫자가 작성되어 있는데, 글자 길이가 5가 되었고 오른쪽 정렬로 실행한 것입니다. 또한 언더바를 맨 뒤에 추가해서 글자 맨 끝에 넣을 수 있습니다. 

 

3. String.format("%-5d_", i)

% 뒤에 '-'를 붙이므로 글자 길이 5에 왼쪽 정렬한 것입니다.

 

4. String.format("%05d_", i)

% 뒤에 0을 붙이면 빈 공간에 0을 채워 넣을 수 있습니다. 제가 다른 것을 넣어보려고 했는데 안 들어가더라고요.

 

 

2.  %s (문자열 형식에 사용)

String str = "hosose";

System.out.println(String.format("%s_", str));
System.out.println(String.format("%12s_", str));
System.out.println(String.format("%-12s❤️", str));
System.out.println(String.format("%.2s_", str));
System.out.println(String.format("%-12.2s_", str));
System.out.println(String.format("%12.2s_", str));

결과

hosose_
      hosose_
hosose      ❤️
ho_
ho          _
          ho_

1. String.format("%s_", str)

글자 마지막에  '_'를 추가하는 형식입니다.

 

2. String.format("%12s_", str)

글자를 12 글자로 만드는 형식입니다.

 

3. String.format("%-12s❤️", str)

'-' 왼쪽 정렬, 길이 12인 문자, 글자 마지막에 하트 넣는 형식입니다.

 

4. String.format("%.2s_", str)

'.' 점이 붙고 글자 수를 지정하면 글자 길이를 그만큼만 보여주게 됩니다.

 

5. String.format("%-12.2s_", str)

왼쪽 정렬, 길이 12 문자, 지정한 문자열에서 2글자만 보여주기, 마지막에 '_' 붙이기

 

6. String.format("%12.2s_", str)

오른쪽 정렬, 길이 12 문자, 지정한 문자열에서 2글자만 보여주기, 마지막에 '_' 붙이기

 

 

3. %f (실수형 숫자 형식에 사용)

double n = 123.45678;

System.out.println(String.format("%f_", n));
System.out.println(String.format("%.8f_", n));
System.out.println(String.format("%15f_", n));
System.out.println(String.format("%-15f_", n));
System.out.println(String.format("%.3f_", n));
System.out.println(String.format("%.2f_", n));
System.out.println(String.format("%15.2f_", n));
System.out.println(String.format("%-15.2f_", n));
System.out.println(String.format("%015f_", n));
System.out.println(String.format("%015.2f_", n));

결과

123.456780_
123.45678000_
     123.456780_
123.456780     _
123.457_
123.46_
         123.46_
123.46         _
00000123.456780_
000000000123.46_

1. String.format("%f_", n)

소수 6자리까지는 default로 보여주는 것 같습니다. 직접 다른 숫자로도 해보면 좋을 것 같아요.

 

2. String.format("%.8f_", n)

% 뒤에 '.' 점을 붙이면서 소수 8자리까지 나타내는 형식입니다.

 

3. String.format("%15f_", n)

전체 글자 수를 15글자로 하는 형식입니다. 여기도 마찬가지로 소수 6자리까지만 나왔습니다

 

4. String.format("%-15f_", n)

글자 수 15글자, 왼쪽 정렬 형식입니다.

 

5. String.format("%.3f_", n)

이건 특이하게 소수점 아래에 있는 숫자 3개만 보여주라는 의미입니다.

아래 %.2f 도 마찬가지로 소수점 아래에 있는 숫자 2개만 보여주는 형식입니다.

 

6. String.format("%15.2f_", n)

글자 수를  15글자로 하고 소수점 밑에 2자리만 보이게 하는 형식입니다. 오른쪽 정렬까지~

 

7. String.format("%-15.2f_", n)

글자 수를  15글자로 하고 소수점 밑에 2자리만 보이게 하는 형식입니다. 왼쪽 정렬까지~

 

8. String.format("%015f_", n)

글자 수를 15글자로 하고 빈 공간에 0을 채워 넣으라는 형식입니다.

 

9. String.format("%015.2f_", n)

글자 수 15글자, 빈 공간에 0을 채우고, 소수점 아래는 2자리만 보이게 만드는 형식입니다.

 

 

소감

자동화하여 숫자를 매기는데 숫자 앞에 0이 필요한 상황이어서 알아보게 되었습니다.

만일 1~40까지 숫자를 매기는데 append 나 '+'로 0을 추가하게 된다면 생각한 대로 결과가 안 나오거나 if 문을 사용하여 변형시켜야 합니다.

001 ~ 009

010 ~ 040

이렇게 나눠서 코드를 작성해야 하는 귀찮은 일이 생기기 때문에 String.format을 이용하여 숫자를 매기는 것이 맞다고 생각했습니다.


출처 : https://blog.jiniworld.me/68

 

[Java] String.format 을 이용한 문자열 형식 설정하기

public static String format(String format, Object... args); public static String format(Locale l, String format, Object... args); String 의 static 메서드인 format 메서드는 문자열의 형식을 설정하는 메서드입니다. %d (10진수 형식

blog.jiniworld.me

반응형