Home > Net >  Kotlin "when in range" not working for negative numbers
Kotlin "when in range" not working for negative numbers

Time:06-15

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

CodePudding user response:

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

  • Related