RaJ
RaJ

Reputation: 1243

Kotlin "when in range" not working for negative numbers

I'm passing -3.0 (Hard coded for my testing purpose) to "when" conditions here result was always comes zero (0 - execute else statement).

Positive numbers working fine, Negative numbers comes that time only not working

Below I'm shared screenshots for reference.

enter image description here

Upvotes: 0

Views: 1581

Answers (1)

Ricky Mo
Ricky Mo

Reputation: 7618

The implementation of ClosedDoubleRange.contains is

override fun contains(value: Double): Boolean = value >= _start && value <= _endInclusive

if start is larger than endInclusive, it always return false. So for negative range, you should write in -3.0..-2.1

Upvotes: 3

Related Questions