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
spand
10/09/2019, 11:51 AM
Works for me:
Copy code
fun main(args: Array<String>) {
println(1.5 in (1.0..2.0))
}
s
Stephan Schroeder
10/09/2019, 12:27 PM
coerceIn
might be interesting to you as well. e.g.
Copy code
val d: Double = getSomeDouble().coerceIn(0.0, 10.0)
@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
}