rangeTo

operator fun <T : Comparable<T>> T.rangeTo(that: T): ClosedRange<T>(source)

Creates a range from this Comparable value to the specified that value.

This value needs to be smaller than or equal to that value, otherwise the returned range will be empty.

Since Kotlin

1.1

Samples

import java.sql.Date
import kotlin.test.assertFalse
import kotlin.test.assertTrue

fun main() { 
   //sampleStart 
   val start = Date.valueOf("2017-01-01")
val end = Date.valueOf("2017-12-31")
val range = start..end
println(range) // 2017-01-01..2017-12-31

println("Date.valueOf(\"2017-05-27\") in range is ${Date.valueOf("2017-05-27") in range}") // true
println("Date.valueOf(\"2018-01-01\") in range is ${Date.valueOf("2018-01-01") in range}") // false
println("Date.valueOf(\"2018-01-01\") !in range is ${Date.valueOf("2018-01-01") !in range}") // true 
   //sampleEnd
}

Creates a range from this Double value to the specified that value.

Numbers are compared with the ends of this range according to IEEE-754.

Since Kotlin

1.1

Samples

import java.sql.Date
import kotlin.test.assertFalse
import kotlin.test.assertTrue

fun main() { 
   //sampleStart 
   val range = 1.0..100.0
println(range) // 1.0..100.0

println("3.14 in range is ${3.14 in range}") // true
println("100.1 in range is ${100.1 in range}") // false 
   //sampleEnd
}

Creates a range from this Float value to the specified that value.

Numbers are compared with the ends of this range according to IEEE-754.

Since Kotlin

1.1

Samples

import java.sql.Date
import kotlin.test.assertFalse
import kotlin.test.assertTrue

fun main() { 
   //sampleStart 
   val range = 1f..100f
println(range) // 1.0..100.0

println("3.14f in range is ${3.14f in range}") // true
println("100.1f in range is ${100.1f in range}") // false 
   //sampleEnd
}
operator fun <T : Comparable<T>> T.rangeTo(that: T): ClosedRange<T>(source)

Creates a range from this Comparable value to the specified that value.

This value needs to be smaller than or equal to that value, otherwise the returned range will be empty.

Since Kotlin

1.0

Samples

import java.sql.Date
import kotlin.test.assertFalse
import kotlin.test.assertTrue

fun main() { 
   //sampleStart 
   val start = Date.valueOf("2017-01-01")
val end = Date.valueOf("2017-12-31")
val range = start..end
println(range) // 2017-01-01..2017-12-31

println("Date.valueOf(\"2017-05-27\") in range is ${Date.valueOf("2017-05-27") in range}") // true
println("Date.valueOf(\"2018-01-01\") in range is ${Date.valueOf("2018-01-01") in range}") // false
println("Date.valueOf(\"2018-01-01\") !in range is ${Date.valueOf("2018-01-01") !in range}") // true 
   //sampleEnd
}

Creates a range from this Double value to the specified that value.

Numbers are compared with the ends of this range according to IEEE-754.

Since Kotlin

1.1

Samples

import java.sql.Date
import kotlin.test.assertFalse
import kotlin.test.assertTrue

fun main() { 
   //sampleStart 
   val range = 1.0..100.0
println(range) // 1.0..100.0

println("3.14 in range is ${3.14 in range}") // true
println("100.1 in range is ${100.1 in range}") // false 
   //sampleEnd
}

Creates a range from this Float value to the specified that value.

Numbers are compared with the ends of this range according to IEEE-754.

Since Kotlin

1.1

Samples

import java.sql.Date
import kotlin.test.assertFalse
import kotlin.test.assertTrue

fun main() { 
   //sampleStart 
   val range = 1f..100f
println(range) // 1.0..100.0

println("3.14f in range is ${3.14f in range}") // true
println("100.1f in range is ${100.1f in range}") // false 
   //sampleEnd
}