Algorithm/알고리즘 설명
[알고리즘] 문자열 → int 형 변환
Sky Titan
2020. 8. 22. 11:20
728x90
import java.util.*;
public class Main {
public static void main(String[] args) {
String str = "12345";
int num = 0;
int multi = 1;
for(int i = str.length()-1;i>=0;i--)
{
num += (str.charAt(i) - 48) * multi;
multi *= 10;
}
System.out.println(num);
}
- multi라는 값을 두어 1의 자리 -> 10의 자리 -> 100의 자리 순으로 숫자를 구할 수 있게 곱해준다.
- 각 수들을 차례대로 결과값에 더한다.
- 최종적으로 5 -> 45 -> 345 -> 2345 -> 12345로 결과값이 변해간다.
728x90