I have this double number that I want to check if ...
# announcements
l
I have this double number that I want to check if is between an interval. I was using "in", but now is marked as deprecated. I understand that there was a mixin creating some confusion. So what is the proper way to do this?
s
Works for me:
Copy code
fun main(args: Array<String>) {
    println(1.5 in (1.0..2.0))
}
s
coerceIn
might be interesting to you as well. e.g.
Copy code
val d: Double = getSomeDouble().coerceIn(0.0, 10.0)
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.ranges/coerce-in.html
l
Johannes Jensen, isn't it marked as deprecated?
s
Clicking on the
in
I get:
Copy code
@SinceKotlin("1.1")
public interface ClosedFloatingPointRange<T : Comparable<T>> : ClosedRange<T> {
    override fun contains(value: T): Boolean = lessThanOrEquals(start, value) && lessThanOrEquals(value, endInclusive)
    override fun isEmpty(): Boolean = !lessThanOrEquals(start, endInclusive)

    /**
     * Compares two values of range domain type and returns true if first is less than or equal to second.
     */
    fun lessThanOrEquals(a: T, b: T): Boolean
}
No deprecations as far as I can see
l
Thanks!