
This assumes Double ranges it could be made to handle other types too, though the type parameters get messy. First, let to see a basic example of a range operator. The range operators can be used with for loops, if conditions or even in when operator. A range can be defined with a start value and an end value with and without inclusion. (This should work for negative numbers and for descending ranges the intersection is always given as ascending. Range Operator in Kotlin is a basic operator that is used to operate over a range. The value which is equal or greater than start value and smaller or equal to end value comes inside the definedrange. Range expressions are created with operator (.) which is complemented by in and in. And having intersect return null if there's no overlap is safer. Kotlin range is defined as an interval from start value to the end value.

Having the size explicit and separate from the intersection makes each one easier to understand, easier to test, and easier to reuse. n 2 Output: 91 Explanation: The answer should be the total numbers in the range of 0 x < 100. With those, you can do e.g.: val a = 22.0. Count number of digits in Kotlin - Stack Overflow. MaxOf(min, other.min).rangeTo(minOf(max, other.max)) = if (min <= other.max & other.min <= max) Infix fun ClosedFloatingPointRange.intersect(other: ClosedFloatingPointRange) So, building on what Adam Millerchip and cactustictacs have already done, I think I'd break the problem down into simpler parts: min, max, and size properties for a range, and an intersect function that calculates the common subrange of two ranges (if there is one): val ClosedFloatingPointRange.min

What you're doing here is taking the intersection (overlap) of two ranges, and calculating its size.
