Kotlin 사칙연산 데이터타입

두리두두·2024년 2월 28일
0

TIL

목록 보기
9/10

형변환

  • answer이 double이라서 첨에 나눗셈할 때
    temp.toDouble() / arr.size.toDouble()했는데 둘 중 하나만 형변환해도 되더라
class Solution {
    fun solution(arr: IntArray): Double {
        var temp = 0
        for (a in arr)
            temp += a
        
        var answer:Double = 0.0
        // 나누기 할 때 하나만 double이어도 된다 ...
        answer = temp / arr.size.toDouble()
        
        return answer
    }
}

코틀린 형변환

toDouble()
toInt()
int.toString()
String to Int

1) String.toInt()

var str: String = "12"
str.toInt()

2) Integer.parseInt(str)

profile
야금야금

0개의 댓글