https://kotlinlang.org logo
#getting-started
Title
# getting-started
d

Dragos Rachieru

06/08/2022, 9:08 AM
Hello, is there a method to calculate a value between a range? Like if I want to get a value that is
X
or
min
or
max
, like:
X=50
,
min=0
,
max=40
, then the result is
40
I need this to calculate some positions, don't know if there's a method using
IntRange
r

Robert Williams

06/08/2022, 9:14 AM
Random.nextInt
can take a (min,max) or an IntRange
s

Sam

06/08/2022, 9:14 AM
Maybe you want
coerceIn
?
Return this value if it's in the range, or
range.start
if this value is less than
range.start
, or
range.endInclusive
if this value is greater than
range.endInclusive
.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.ranges/coerce-in.html
🙏 1
2
r

Robert Williams

06/08/2022, 9:15 AM
Not sure what's the purpose of
X
in your example 🤔
Ah yeah,
coerceIn
makes more sense, not sure where I got the idea you wanted a random value homer disappear
d

Dragos Rachieru

06/08/2022, 9:24 AM
Thank you,
coerceIn
is the extension I was looking for
2 Views