import kotlin.math.*
주어진 값 x 를 양의 무한대 방향의 정수로 반올림합니다.
public actual inline fun ceil(x: Double): Double = nativeMath.ceil(x)
println(ceil(16.746)) // 17.0
주어진 값 x 를 음의 무한대 방향의 정수로 반올림합니다.
public actual inline fun floor(x: Double): Double = nativeMath.floor(x)
println(floor(16.746)) // 17.0
지정된 값 x를 짝수 방향으로 반올림하여 가장 가까운 정수 방향으로 반올림합니다.
public actual fun round(x: Double): Double {
if (x % 0.5 != 0.0) {
return nativeMath.round(x)
}
val floor = floor(x)
return if (floor % 2 == 0.0) floor else ceil(x)
}
println(round(16.5)) // 16.0
println((16.746).roundToInt()) // 17
println((16.746).roundToLong()) // 17
println("소수점 아래 2번 째에서 반올림 : ${round(16.746 * 10) / 10}") // 16.7
println("소수점 아래 3번 째에서 반올림 : ${round(16.746 * 100) / 100}") // 16.75
'코틀린 > etc.' 카테고리의 다른 글
[Kotlin][Comparison Operation] compareTo (0) | 2024.09.04 |
---|---|
[Kotlin][Transformation Operation] sqrt / pow / hypot (0) | 2024.09.02 |
[Kotlin][Iteration Operation] repeat (0) | 2024.08.27 |