본문 바로가기
코틀린/[Filtering] 필터 작업

[Kotlin][Number] coerceAtMost

by jinwo_o 2024. 9. 2.

maximumValue 보다 작거나 같으면 이 값을 반환하고, 그렇지 않으면 maximumValue 를 반환합니다.

public fun <T : Comparable<T>> T.coerceAtMost(maximumValue: T): T {
    return if (this > maximumValue) maximumValue else this
}


println(DayOfWeek.FRIDAY.coerceAtMost(DayOfWeek.SATURDAY)) // FRIDAY
println(DayOfWeek.FRIDAY.coerceAtMost(DayOfWeek.WEDNESDAY)) // WEDNESDAY

println(10.coerceAtMost(5)) // 5
println(10.coerceAtMost(20)) // 10

 

coerceAtMost - Kotlin Programming Language

 

kotlinlang.org