# 자바 printf() 사용법 질문드립니다!
## 형식 문자열 출력하기 - printf() `printf()` 메소드는, 문자열 **형식**에 **변수를 삽입**하여 출력합니다. ## 형식 종류 - `%s`: 문자열(string) - `%d`: 십진수(decimal) - `%f`: 실수(float) ## 코드 예 1) 변수를 형식으로 삽입 ``` String id = "SuperMan"; double score = 96.4; int rank = 2; System.out.printf("아이디: %s, 점수: %f, 순위: %d\n", id, score, rank); # => 아이디: SuperMan, 점수: 96.400000, 순위: 2 ``` 2) 소수점 이하 자릿수 제한 ``` // 원의 넓이 = 원주율 * 반지름 * 반지름 int radius = 2; double area = Math.PI * radius * radius; // 기본 출력: 12.566371 System.out.printf("%f\n", area); // 소수점 제한: 12.57 System.out.printf("%.2f\n", area); ``` ## 참고자료 --- 1. https://goo.gl/stv9qD