roundToLong

expect fun Double.roundToLong(): Long(source)

Rounds this Double value to the nearest integer and converts the result to Long. Ties are rounded towards positive infinity.

Special cases:

  • x.roundToLong() == Long.MAX_VALUE when x > Long.MAX_VALUE

  • x.roundToLong() == Long.MIN_VALUE when x < Long.MIN_VALUE

Since Kotlin

1.2

Throws

when this value is NaN

Samples

import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   println(3.14159.roundToLong()) // 3
println((-10.0).roundToLong()) // -10
// Values greater than Long.MAX_VALUE are rounded to Long.MAX_VALUE
println("(Long.MAX_VALUE.toDouble() * 2.0).roundToLong() == Long.MAX_VALUE is ${(Long.MAX_VALUE.toDouble() * 2.0).roundToLong() == Long.MAX_VALUE}") // true
println("Double.POSITIVE_INFINITY.roundToLong() == Long.MAX_VALUE is ${Double.POSITIVE_INFINITY.roundToLong() == Long.MAX_VALUE}") // true
// Values smaller than Long.MIN_VALUE are rounded to Long.MIN_VALUE
println("(Long.MIN_VALUE.toDouble() * 2.0).roundToLong() == Long.MIN_VALUE is ${(Long.MIN_VALUE.toDouble() * 2.0).roundToLong() == Long.MIN_VALUE}") // true
println("Double.NEGATIVE_INFINITY.roundToLong() == Long.MIN_VALUE is ${Double.NEGATIVE_INFINITY.roundToLong() == Long.MIN_VALUE}") // true
// NaN could not be rounded to Long
// Double.NaN.roundToLong() // will fail with IllegalArgumentException 
   //sampleEnd
}

expect fun Float.roundToLong(): Long(source)

Rounds this Float value to the nearest integer and converts the result to Long. Ties are rounded towards positive infinity.

Special cases:

  • x.roundToLong() == Long.MAX_VALUE when x > Long.MAX_VALUE

  • x.roundToLong() == Long.MIN_VALUE when x < Long.MIN_VALUE

Since Kotlin

1.2

Throws

when this value is NaN

Samples

import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   println(3.14159f.roundToLong()) // 3
println((-10.0f).roundToLong()) // -10
// Values greater than Long.MAX_VALUE are rounded to Long.MAX_VALUE
println("(Long.MAX_VALUE.toFloat() * 2.0f).roundToLong() == Long.MAX_VALUE is ${(Long.MAX_VALUE.toFloat() * 2.0f).roundToLong() == Long.MAX_VALUE}") // true
println("Float.POSITIVE_INFINITY.roundToLong() == Long.MAX_VALUE is ${Float.POSITIVE_INFINITY.roundToLong() == Long.MAX_VALUE}") // true
// Values smaller than Long.MIN_VALUE are rounded to Long.MIN_VALUE
println("(Long.MIN_VALUE.toFloat() * 2.0f).roundToLong() == Long.MIN_VALUE is ${(Long.MIN_VALUE.toFloat() * 2.0f).roundToLong() == Long.MIN_VALUE}") // true
println("Float.NEGATIVE_INFINITY.roundToLong() == Long.MIN_VALUE is ${Float.NEGATIVE_INFINITY.roundToLong() == Long.MIN_VALUE}") // true
// NaN could not be rounded to Long
// Float.NaN.roundToLong() // will fail with IllegalArgumentException 
   //sampleEnd
}
actual fun Double.roundToLong(): Long(source)

Rounds this Double value to the nearest integer and converts the result to Long. Ties are rounded towards positive infinity.

Special cases:

  • x.roundToLong() == Long.MAX_VALUE when x > Long.MAX_VALUE

  • x.roundToLong() == Long.MIN_VALUE when x < Long.MIN_VALUE

Since Kotlin

1.2

Throws

when this value is NaN

Samples

import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   println(3.14159.roundToLong()) // 3
println((-10.0).roundToLong()) // -10
// Values greater than Long.MAX_VALUE are rounded to Long.MAX_VALUE
println("(Long.MAX_VALUE.toDouble() * 2.0).roundToLong() == Long.MAX_VALUE is ${(Long.MAX_VALUE.toDouble() * 2.0).roundToLong() == Long.MAX_VALUE}") // true
println("Double.POSITIVE_INFINITY.roundToLong() == Long.MAX_VALUE is ${Double.POSITIVE_INFINITY.roundToLong() == Long.MAX_VALUE}") // true
// Values smaller than Long.MIN_VALUE are rounded to Long.MIN_VALUE
println("(Long.MIN_VALUE.toDouble() * 2.0).roundToLong() == Long.MIN_VALUE is ${(Long.MIN_VALUE.toDouble() * 2.0).roundToLong() == Long.MIN_VALUE}") // true
println("Double.NEGATIVE_INFINITY.roundToLong() == Long.MIN_VALUE is ${Double.NEGATIVE_INFINITY.roundToLong() == Long.MIN_VALUE}") // true
// NaN could not be rounded to Long
// Double.NaN.roundToLong() // will fail with IllegalArgumentException 
   //sampleEnd
}

actual inline fun Float.roundToLong(): Long(source)

Rounds this Float value to the nearest integer and converts the result to Long. Ties are rounded towards positive infinity.

Special cases:

  • x.roundToLong() == Long.MAX_VALUE when x > Long.MAX_VALUE

  • x.roundToLong() == Long.MIN_VALUE when x < Long.MIN_VALUE

Since Kotlin

1.2

Throws

when this value is NaN

Samples

import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   println(3.14159f.roundToLong()) // 3
println((-10.0f).roundToLong()) // -10
// Values greater than Long.MAX_VALUE are rounded to Long.MAX_VALUE
println("(Long.MAX_VALUE.toFloat() * 2.0f).roundToLong() == Long.MAX_VALUE is ${(Long.MAX_VALUE.toFloat() * 2.0f).roundToLong() == Long.MAX_VALUE}") // true
println("Float.POSITIVE_INFINITY.roundToLong() == Long.MAX_VALUE is ${Float.POSITIVE_INFINITY.roundToLong() == Long.MAX_VALUE}") // true
// Values smaller than Long.MIN_VALUE are rounded to Long.MIN_VALUE
println("(Long.MIN_VALUE.toFloat() * 2.0f).roundToLong() == Long.MIN_VALUE is ${(Long.MIN_VALUE.toFloat() * 2.0f).roundToLong() == Long.MIN_VALUE}") // true
println("Float.NEGATIVE_INFINITY.roundToLong() == Long.MIN_VALUE is ${Float.NEGATIVE_INFINITY.roundToLong() == Long.MIN_VALUE}") // true
// NaN could not be rounded to Long
// Float.NaN.roundToLong() // will fail with IllegalArgumentException 
   //sampleEnd
}
actual fun Double.roundToLong(): Long(source)

Rounds this Double value to the nearest integer and converts the result to Long. Ties are rounded towards positive infinity.

Special cases:

  • x.roundToLong() == Long.MAX_VALUE when x > Long.MAX_VALUE

  • x.roundToLong() == Long.MIN_VALUE when x < Long.MIN_VALUE

Since Kotlin

1.2

Throws

when this value is NaN

Samples

import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   println(3.14159.roundToLong()) // 3
println((-10.0).roundToLong()) // -10
// Values greater than Long.MAX_VALUE are rounded to Long.MAX_VALUE
println("(Long.MAX_VALUE.toDouble() * 2.0).roundToLong() == Long.MAX_VALUE is ${(Long.MAX_VALUE.toDouble() * 2.0).roundToLong() == Long.MAX_VALUE}") // true
println("Double.POSITIVE_INFINITY.roundToLong() == Long.MAX_VALUE is ${Double.POSITIVE_INFINITY.roundToLong() == Long.MAX_VALUE}") // true
// Values smaller than Long.MIN_VALUE are rounded to Long.MIN_VALUE
println("(Long.MIN_VALUE.toDouble() * 2.0).roundToLong() == Long.MIN_VALUE is ${(Long.MIN_VALUE.toDouble() * 2.0).roundToLong() == Long.MIN_VALUE}") // true
println("Double.NEGATIVE_INFINITY.roundToLong() == Long.MIN_VALUE is ${Double.NEGATIVE_INFINITY.roundToLong() == Long.MIN_VALUE}") // true
// NaN could not be rounded to Long
// Double.NaN.roundToLong() // will fail with IllegalArgumentException 
   //sampleEnd
}

actual fun Float.roundToLong(): Long(source)

Rounds this Float value to the nearest integer and converts the result to Long. Ties are rounded towards positive infinity.

Special cases:

  • x.roundToLong() == Long.MAX_VALUE when x > Long.MAX_VALUE

  • x.roundToLong() == Long.MIN_VALUE when x < Long.MIN_VALUE

Since Kotlin

1.2

Throws

when this value is NaN

Samples

import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   println(3.14159f.roundToLong()) // 3
println((-10.0f).roundToLong()) // -10
// Values greater than Long.MAX_VALUE are rounded to Long.MAX_VALUE
println("(Long.MAX_VALUE.toFloat() * 2.0f).roundToLong() == Long.MAX_VALUE is ${(Long.MAX_VALUE.toFloat() * 2.0f).roundToLong() == Long.MAX_VALUE}") // true
println("Float.POSITIVE_INFINITY.roundToLong() == Long.MAX_VALUE is ${Float.POSITIVE_INFINITY.roundToLong() == Long.MAX_VALUE}") // true
// Values smaller than Long.MIN_VALUE are rounded to Long.MIN_VALUE
println("(Long.MIN_VALUE.toFloat() * 2.0f).roundToLong() == Long.MIN_VALUE is ${(Long.MIN_VALUE.toFloat() * 2.0f).roundToLong() == Long.MIN_VALUE}") // true
println("Float.NEGATIVE_INFINITY.roundToLong() == Long.MIN_VALUE is ${Float.NEGATIVE_INFINITY.roundToLong() == Long.MIN_VALUE}") // true
// NaN could not be rounded to Long
// Float.NaN.roundToLong() // will fail with IllegalArgumentException 
   //sampleEnd
}
actual fun Double.roundToLong(): Long(source)

Rounds this Double value to the nearest integer and converts the result to Long. Ties are rounded towards positive infinity.

Special cases:

  • x.roundToLong() == Long.MAX_VALUE when x > Long.MAX_VALUE

  • x.roundToLong() == Long.MIN_VALUE when x < Long.MIN_VALUE

Since Kotlin

1.3

Throws

when this value is NaN

Samples

import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   println(3.14159.roundToLong()) // 3
println((-10.0).roundToLong()) // -10
// Values greater than Long.MAX_VALUE are rounded to Long.MAX_VALUE
println("(Long.MAX_VALUE.toDouble() * 2.0).roundToLong() == Long.MAX_VALUE is ${(Long.MAX_VALUE.toDouble() * 2.0).roundToLong() == Long.MAX_VALUE}") // true
println("Double.POSITIVE_INFINITY.roundToLong() == Long.MAX_VALUE is ${Double.POSITIVE_INFINITY.roundToLong() == Long.MAX_VALUE}") // true
// Values smaller than Long.MIN_VALUE are rounded to Long.MIN_VALUE
println("(Long.MIN_VALUE.toDouble() * 2.0).roundToLong() == Long.MIN_VALUE is ${(Long.MIN_VALUE.toDouble() * 2.0).roundToLong() == Long.MIN_VALUE}") // true
println("Double.NEGATIVE_INFINITY.roundToLong() == Long.MIN_VALUE is ${Double.NEGATIVE_INFINITY.roundToLong() == Long.MIN_VALUE}") // true
// NaN could not be rounded to Long
// Double.NaN.roundToLong() // will fail with IllegalArgumentException 
   //sampleEnd
}

actual fun Float.roundToLong(): Long(source)

Rounds this Float value to the nearest integer and converts the result to Long. Ties are rounded towards positive infinity.

Special cases:

  • x.roundToLong() == Long.MAX_VALUE when x > Long.MAX_VALUE

  • x.roundToLong() == Long.MIN_VALUE when x < Long.MIN_VALUE

Since Kotlin

1.3

Throws

when this value is NaN

Samples

import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   println(3.14159f.roundToLong()) // 3
println((-10.0f).roundToLong()) // -10
// Values greater than Long.MAX_VALUE are rounded to Long.MAX_VALUE
println("(Long.MAX_VALUE.toFloat() * 2.0f).roundToLong() == Long.MAX_VALUE is ${(Long.MAX_VALUE.toFloat() * 2.0f).roundToLong() == Long.MAX_VALUE}") // true
println("Float.POSITIVE_INFINITY.roundToLong() == Long.MAX_VALUE is ${Float.POSITIVE_INFINITY.roundToLong() == Long.MAX_VALUE}") // true
// Values smaller than Long.MIN_VALUE are rounded to Long.MIN_VALUE
println("(Long.MIN_VALUE.toFloat() * 2.0f).roundToLong() == Long.MIN_VALUE is ${(Long.MIN_VALUE.toFloat() * 2.0f).roundToLong() == Long.MIN_VALUE}") // true
println("Float.NEGATIVE_INFINITY.roundToLong() == Long.MIN_VALUE is ${Float.NEGATIVE_INFINITY.roundToLong() == Long.MIN_VALUE}") // true
// NaN could not be rounded to Long
// Float.NaN.roundToLong() // will fail with IllegalArgumentException 
   //sampleEnd
}
actual fun Double.roundToLong(): Long(source)

Rounds this Double value to the nearest integer and converts the result to Long. Ties are rounded towards positive infinity.

Special cases:

  • x.roundToLong() == Long.MAX_VALUE when x > Long.MAX_VALUE

  • x.roundToLong() == Long.MIN_VALUE when x < Long.MIN_VALUE

Since Kotlin

1.8

Throws

when this value is NaN

Samples

import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   println(3.14159.roundToLong()) // 3
println((-10.0).roundToLong()) // -10
// Values greater than Long.MAX_VALUE are rounded to Long.MAX_VALUE
println("(Long.MAX_VALUE.toDouble() * 2.0).roundToLong() == Long.MAX_VALUE is ${(Long.MAX_VALUE.toDouble() * 2.0).roundToLong() == Long.MAX_VALUE}") // true
println("Double.POSITIVE_INFINITY.roundToLong() == Long.MAX_VALUE is ${Double.POSITIVE_INFINITY.roundToLong() == Long.MAX_VALUE}") // true
// Values smaller than Long.MIN_VALUE are rounded to Long.MIN_VALUE
println("(Long.MIN_VALUE.toDouble() * 2.0).roundToLong() == Long.MIN_VALUE is ${(Long.MIN_VALUE.toDouble() * 2.0).roundToLong() == Long.MIN_VALUE}") // true
println("Double.NEGATIVE_INFINITY.roundToLong() == Long.MIN_VALUE is ${Double.NEGATIVE_INFINITY.roundToLong() == Long.MIN_VALUE}") // true
// NaN could not be rounded to Long
// Double.NaN.roundToLong() // will fail with IllegalArgumentException 
   //sampleEnd
}

actual fun Float.roundToLong(): Long(source)

Rounds this Float value to the nearest integer and converts the result to Long. Ties are rounded towards positive infinity.

Special cases:

  • x.roundToLong() == Long.MAX_VALUE when x > Long.MAX_VALUE

  • x.roundToLong() == Long.MIN_VALUE when x < Long.MIN_VALUE

Since Kotlin

1.8

Throws

when this value is NaN

Samples

import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   println(3.14159f.roundToLong()) // 3
println((-10.0f).roundToLong()) // -10
// Values greater than Long.MAX_VALUE are rounded to Long.MAX_VALUE
println("(Long.MAX_VALUE.toFloat() * 2.0f).roundToLong() == Long.MAX_VALUE is ${(Long.MAX_VALUE.toFloat() * 2.0f).roundToLong() == Long.MAX_VALUE}") // true
println("Float.POSITIVE_INFINITY.roundToLong() == Long.MAX_VALUE is ${Float.POSITIVE_INFINITY.roundToLong() == Long.MAX_VALUE}") // true
// Values smaller than Long.MIN_VALUE are rounded to Long.MIN_VALUE
println("(Long.MIN_VALUE.toFloat() * 2.0f).roundToLong() == Long.MIN_VALUE is ${(Long.MIN_VALUE.toFloat() * 2.0f).roundToLong() == Long.MIN_VALUE}") // true
println("Float.NEGATIVE_INFINITY.roundToLong() == Long.MIN_VALUE is ${Float.NEGATIVE_INFINITY.roundToLong() == Long.MIN_VALUE}") // true
// NaN could not be rounded to Long
// Float.NaN.roundToLong() // will fail with IllegalArgumentException 
   //sampleEnd
}
actual fun Double.roundToLong(): Long(source)

Rounds this Double value to the nearest integer and converts the result to Long. Ties are rounded towards positive infinity.

Special cases:

  • x.roundToLong() == Long.MAX_VALUE when x > Long.MAX_VALUE

  • x.roundToLong() == Long.MIN_VALUE when x < Long.MIN_VALUE

Since Kotlin

1.8

Throws

when this value is NaN

Samples

import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   println(3.14159.roundToLong()) // 3
println((-10.0).roundToLong()) // -10
// Values greater than Long.MAX_VALUE are rounded to Long.MAX_VALUE
println("(Long.MAX_VALUE.toDouble() * 2.0).roundToLong() == Long.MAX_VALUE is ${(Long.MAX_VALUE.toDouble() * 2.0).roundToLong() == Long.MAX_VALUE}") // true
println("Double.POSITIVE_INFINITY.roundToLong() == Long.MAX_VALUE is ${Double.POSITIVE_INFINITY.roundToLong() == Long.MAX_VALUE}") // true
// Values smaller than Long.MIN_VALUE are rounded to Long.MIN_VALUE
println("(Long.MIN_VALUE.toDouble() * 2.0).roundToLong() == Long.MIN_VALUE is ${(Long.MIN_VALUE.toDouble() * 2.0).roundToLong() == Long.MIN_VALUE}") // true
println("Double.NEGATIVE_INFINITY.roundToLong() == Long.MIN_VALUE is ${Double.NEGATIVE_INFINITY.roundToLong() == Long.MIN_VALUE}") // true
// NaN could not be rounded to Long
// Double.NaN.roundToLong() // will fail with IllegalArgumentException 
   //sampleEnd
}

actual fun Float.roundToLong(): Long(source)

Rounds this Float value to the nearest integer and converts the result to Long. Ties are rounded towards positive infinity.

Special cases:

  • x.roundToLong() == Long.MAX_VALUE when x > Long.MAX_VALUE

  • x.roundToLong() == Long.MIN_VALUE when x < Long.MIN_VALUE

Since Kotlin

1.8

Throws

when this value is NaN

Samples

import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   println(3.14159f.roundToLong()) // 3
println((-10.0f).roundToLong()) // -10
// Values greater than Long.MAX_VALUE are rounded to Long.MAX_VALUE
println("(Long.MAX_VALUE.toFloat() * 2.0f).roundToLong() == Long.MAX_VALUE is ${(Long.MAX_VALUE.toFloat() * 2.0f).roundToLong() == Long.MAX_VALUE}") // true
println("Float.POSITIVE_INFINITY.roundToLong() == Long.MAX_VALUE is ${Float.POSITIVE_INFINITY.roundToLong() == Long.MAX_VALUE}") // true
// Values smaller than Long.MIN_VALUE are rounded to Long.MIN_VALUE
println("(Long.MIN_VALUE.toFloat() * 2.0f).roundToLong() == Long.MIN_VALUE is ${(Long.MIN_VALUE.toFloat() * 2.0f).roundToLong() == Long.MIN_VALUE}") // true
println("Float.NEGATIVE_INFINITY.roundToLong() == Long.MIN_VALUE is ${Float.NEGATIVE_INFINITY.roundToLong() == Long.MIN_VALUE}") // true
// NaN could not be rounded to Long
// Float.NaN.roundToLong() // will fail with IllegalArgumentException 
   //sampleEnd
}