W3cubDocs

/Kotlin

coerceAtLeast

Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)
fun <T : Comparable<T>> T.coerceAtLeast(minimumValue: T): T

Ensures that this value is not less than the specified minimumValue.

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

fun main(args: Array<String>) {
//sampleStart
println(DayOfWeek.WEDNESDAY.coerceAtLeast(DayOfWeek.MONDAY)) // WEDNESDAY
println(DayOfWeek.WEDNESDAY.coerceAtLeast(DayOfWeek.FRIDAY)) // FRIDAY
//sampleEnd
}

Return this value if it's greater than or equal to the minimumValue or the minimumValue otherwise.

Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)
fun Byte.coerceAtLeast(minimumValue: Byte): Byte
fun Short.coerceAtLeast(minimumValue: Short): Short
fun Int.coerceAtLeast(minimumValue: Int): Int
fun Long.coerceAtLeast(minimumValue: Long): Long
fun Float.coerceAtLeast(minimumValue: Float): Float
fun Double.coerceAtLeast(minimumValue: Double): Double

Ensures that this value is not less than the specified minimumValue.

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

fun main(args: Array<String>) {
//sampleStart
println(10.coerceAtLeast(5)) // 10
println(10.coerceAtLeast(20)) // 20
//sampleEnd
}

Return this value if it's greater than or equal to the minimumValue or the minimumValue otherwise.

Platform and version requirements: JVM (1.3), JS (1.3), Native (1.3)
@ExperimentalUnsignedTypes fun UInt.coerceAtLeast(
    minimumValue: UInt
): UInt
@ExperimentalUnsignedTypes fun ULong.coerceAtLeast(
    minimumValue: ULong
): ULong
@ExperimentalUnsignedTypes fun UByte.coerceAtLeast(
    minimumValue: UByte
): UByte
@ExperimentalUnsignedTypes fun UShort.coerceAtLeast(
    minimumValue: UShort
): UShort

Ensures that this value is not less than the specified minimumValue.

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

fun main(args: Array<String>) {
//sampleStart
println(10u.coerceAtLeast(5u)) // 10
println(10u.coerceAtLeast(20u)) // 20
//sampleEnd
}

Return this value if it's greater than or equal to the minimumValue or the minimumValue otherwise.

© 2010–2020 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.ranges/coerce-at-least.html