W3cubDocs

/Kotlin

coerceIn

fun <T : Comparable<T>> T.coerceIn(
    minimumValue: T?, 
    maximumValue: T?
): T
fun Byte.coerceIn(
    minimumValue: Byte, 
    maximumValue: Byte
): Byte
fun Short.coerceIn(
    minimumValue: Short, 
    maximumValue: Short
): Short
fun Int.coerceIn(minimumValue: Int, maximumValue: Int): Int
fun Long.coerceIn(
    minimumValue: Long, 
    maximumValue: Long
): Long
fun Float.coerceIn(
    minimumValue: Float, 
    maximumValue: Float
): Float
fun Double.coerceIn(
    minimumValue: Double, 
    maximumValue: Double
): Double

Ensures that this value lies in the specified range minimumValue..maximumValue.

Return this value if it's in the range, or minimumValue if this value is less than minimumValue, or maximumValue if this value is greater than maximumValue.

fun <T : Comparable<T>> T.coerceIn(
    range: ClosedFloatingPointRange<T>
): T

Platform and version requirements: Kotlin 1.1

fun <T : Comparable<T>> T.coerceIn(range: ClosedRange<T>): T
fun Int.coerceIn(range: ClosedRange<Int>): Int
fun Long.coerceIn(range: ClosedRange<Long>): Long

Ensures that this value lies in the specified range.

Return this value if it's in the range, or range.start if this value is less than range.start, or range.endInclusive if this value is greater than range.endInclusive.

© 2010–2017 JetBrains s.r.o.
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.ranges/coerce-in.html