> For the complete documentation index, see [llms.txt](https://heunnajo.gitbook.io/java/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://heunnajo.gitbook.io/java/ch2/printf.md).

# printf and formatter

* println()의 단점 : 출력형식 지정불가

1. 실수의 자리수 조절 불가(소수점 n자리까지 출력할 수 없다.)

```java
System.out.prinln(10.0/3);//3.333333...
```

2\. 10진수로만 출력한다.

```java
System.out.prinln(0x1A);//26
```

* printf()로 출력형식 지정가능 : 지시자(%f, %d, %X,...)

1. 소수점 둘째자리까지 출력

```java
System.out.prinf("%.2f",10.0/3);//3.33
```

2\. 10진수로 출력

```java
System.out.prinf("%d",0x1A);//26
```

3\. 16진수로 출력

```java
System.out.prinf("%X",0x1A);//1A
```

* Useful Formatter of printf() : 자주 쓰이는 printf()의 지시자

| Formatter | Description                        |
| --------- | ---------------------------------- |
| %b        | boolean 형식으로 출력                    |
| %d        | decimal(10진) 형식으로 출력               |
| %o        | octal(8진) 형식으로 출력                  |
| %x, %X    | hexa-decimal(16진) 형식으로 출력          |
| %f        | floating-point(부동소수점;떠다니는) 형식으로 출력 |
| %e, %E    | exponent(지수) 형식으로 출력               |
| %c        | character(문자) 형식으로 출력              |
| %s        | string(문자열) 형식으로 출력                |

```java
//8진수, 16진수에 접두사가 붙이기
System.out.prinf("%#o",15);//017
System.out.prinf("%#x",15);//0xf
System.out.prinf("%#X",15);//0Xf
```

실수 출력을 위한 지시자 %f - 지수형식(%e), 간략한 형식(%g)

실수를 표현할 땐 %f를 쓰고, 0이 많이 들어간다면 %e(지수형식)을 많이 쓴다.

```java
float f = 123.4567890f;

//float은 정밀도가 7자리이므로 소수점을 포함한 7자리까지만 의미있고 그 이하로는 부정확한 값이다.
System.out.printf("%f", f);//123.456787. 소수점아래 6자리까지 보여준다.
System.out.printf("%e", f);//1.234568e+02. '8'은 반올림 되었고, e+02 = 10^2이다.
```

간략한 형식(%g)

```java
System.out.prinf("%g",123.456789);//123.457 소수점을 포함하여 7자리로 보여준다.%f처럼
System.out.prinf("%g",0.0000001);//1.00000e-8 지수로 표현하는 게 더 간략해서 %e처럼 지수로 표현.
```

더 많은 formatter를 확인하려면 Java API 문서에서 Formatter를 검색해서 참고하면 된다.&#x20;

prinln은 자동 줄바꿈이 되지만, printf는 %n이나 \n 같은 개행문자를 넣어줘야한다. %n은 OS에 상관없이 적용되므로 %n을 사용하도록 하는 것이 좋다.

```java
//정수 
System.out.printf("[%5d]%n",10);//_ _ _ 1 0;오른쪽 정렬 
System.out.printf("[%-5d]%n",10);//1 0 _ _ ;왼쪽 정렬 
System.out.printf("[%05d]%n",10);//00010출력 
System.out.printf("[%5d]%n",1234567);//1234567출력. 5자리로 지정했지만 넘기 때문에 그냥 다 출력된다.
		
//실수 
double d = 1.23456789;
System.out.printf("%f%n",d);//1.234568
System.out.printf("%14.10f%n",d);//소수점포함 전체 14자리, 소수점 아래 10자리.정수부분은 공백문자, 소수점 부분은 0으로 채워진다.
System.out.printf("%.6f%n",d);//소수점 아래 6자리까지표현 
		
//문자열 
System.out.printf("[%s]%n","www.codechobo.com");
System.out.printf("[%20s]%n","www.codechobo.com");//20자리 지정 
System.out.printf("[%-20s]%n","www.codechobo.com");//20자리 지정, 왼쪽 정렬 
System.out.printf("[%.10s]%n","www.codechobo.com");//10자리까지만 잘라서 출력 가능
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://heunnajo.gitbook.io/java/ch2/printf.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
