Is there any way to create double or float range f...
# getting-started
v
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
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
thanks. awesome. Yeah. It's closed range:
Copy code
@SinceKotlin("1.1")
public operator fun Double.rangeTo(that: Double): ClosedFloatingPointRange<Double> = ClosedDoubleRange(this, that)