coerceAtMost (original) (raw)

Ensures that this value is not greater than the specified maximumValue.

Since Kotlin

1.0

Return

this value if it's less than or equal to the maximumValue or the maximumValue otherwise.

Samples

import java.time.DayOfWeek
import kotlin.test.assertFailsWith

fun main() { 
   //sampleStart 
   println(DayOfWeek.FRIDAY.coerceAtMost(DayOfWeek.SATURDAY)) // FRIDAY
println(DayOfWeek.FRIDAY.coerceAtMost(DayOfWeek.WEDNESDAY)) // WEDNESDAY 
   //sampleEnd
}

Ensures that this value is not greater than the specified maximumValue.

Since Kotlin

1.0

Return

this value if it's less than or equal to the maximumValue or the maximumValue otherwise.

Samples

import java.time.DayOfWeek
import kotlin.test.assertFailsWith

fun main() { 
   //sampleStart 
   println(10.coerceAtMost(5)) // 5
println(10.coerceAtMost(20)) // 10 
   //sampleEnd
}

Ensures that this value is not greater than the specified maximumValue.

Since Kotlin

1.5

Return

this value if it's less than or equal to the maximumValue or the maximumValue otherwise.

Samples

import java.time.DayOfWeek
import kotlin.test.assertFailsWith

fun main() { 
   //sampleStart 
   println(10u.coerceAtMost(5u)) // 5
println(10u.coerceAtMost(20u)) // 10 
   //sampleEnd
}