https://kotlinlang.org logo
Title
v

vitaly

02/04/2020, 6:28 PM
Is there any way to create double or float range from String? I have strings like "0.1-0.5", "5.5-5.8", etc. in my configuration and I want to transform them to ClosedFloatRange/ClosedDoubleRange. I could create an extention function for String type but cannot instantiate those types as they are private. Also I wonder why integer ranges (IntRange,LongRange and so on) are public but floating point ranges are not.
a

Alowaniak

02/05/2020, 6:56 AM
fun String.toClosedRange() = split("-").let { (a, b) -> a.toDouble() .. b.toDouble() }
oh wait, i'm actually not sure if it's a closed range, but I think so? Also no idea why some of the types are public and some private, but generally you would create ranges with the
rangeTo
I think?
👍 2
v

vitaly

02/05/2020, 7:06 AM
thanks. awesome. Yeah. It's closed range:
@SinceKotlin("1.1")
public operator fun Double.rangeTo(that: Double): ClosedFloatingPointRange<Double> = ClosedDoubleRange(this, that)