Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 2진수
- SaaS
- Python
- 백준
- level1
- algorithm
- 짝수
- 데이터타입
- java
- 알고리즘
- IaaS
- 온프레미스
- 리스트
- PaaS
- 문자열 숫자 변환
- 홀수
- 최대공배수
- 최대공약수
- 11004
- INT
- 유클리드 호제법
- 웹 서버
- 자료형
- aws
- IntelliJ
- 프로젝트 생성
- valueof
- 프로그래머스
- 11652
- parseInt
Archives
- Today
- Total
Ga0Lee
[Java] 정수를 문자열로 변환하기/int를 String으로 변환 본문
Java에서 int와 long 데이터를 문자열로 바꾸는 방법에는 네 가지가 있다.
1. Integer.toString()
Integer 클래스의 메소드이다.
int형 파라미터 i를 입력받으면 문자열을 반환한다.
public static String toString(int i) {
int size = stringSize(i);
if (COMPACT_STRINGS) {
byte[] buf = new byte[size];
getChars(i, size, buf);
return new String(buf, LATIN1);
} else {
byte[] buf = new byte[size * 2];
StringUTF16.getChars(i, size, buf);
return new String(buf, UTF16);
}
}
예제
int num = 100;
String str = Integer.toString(num); 100 -> "100"으로 변환
2. int + ""
int형 데이터에 문자열을 이어붙이면 문자열이 반환된다.
예제
int num = 100;
String str = num + ""; 100-> "100"으로 변환
3. String.valueOf()
String 클래스의 메소드이다.
int형 파라미터 i를 입력받으면 int형 i를 반환한다.
public static String valueOf(int i) {
return Integer.toString(i);
}
예제
int num = 100;
String str = String.valueOf(num); 100 -> "100" 으로 변환
4. String.format()
String 클래스의 메소드이다.
format에 "%d", args에 int형 데이터를 전달하면 int형 args를 반환한다.
public static String format(String format, Object... args) {
return new Formatter().format(format, args).toString();
}
예제
int num = 100;
String str = String.format("%d", num); 100 -> "100"으로 변환
'Java' 카테고리의 다른 글
[Java] 배열 정렬하기/오름차순, 내림차순 - Arrays.sort() / primitive type, Object class, boxing (1) | 2022.06.23 |
---|---|
[Java] 문자열을 배열로, 배열을 문자열로 변환하기/String.split(), String.join() (0) | 2022.06.18 |
[Java] 제곱 연산하기 Math.pow() (0) | 2022.06.12 |
[Java] 제곱근(루트) 구하기 Math.sqrt() (0) | 2022.06.12 |
[Java] 문자열 반복(곱하기) 메소드 repeat (0) | 2022.06.12 |