roundToInt

expect fun Double.roundToInt(): Int(source)

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

Special cases:

  • x.roundToInt() == Int.MAX_VALUE when x > Int.MAX_VALUE

  • x.roundToInt() == Int.MIN_VALUE when x < Int.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.roundToInt()) // 3
println((-10.0).roundToInt()) // -10
// Values greater than Int.MAX_VALUE are rounded to Int.MAX_VALUE
println("(Int.MAX_VALUE.toDouble() + 1.0).roundToInt() == Int.MAX_VALUE is ${(Int.MAX_VALUE.toDouble() + 1.0).roundToInt() == Int.MAX_VALUE}") // true
println("Double.POSITIVE_INFINITY.roundToInt() == Int.MAX_VALUE is ${Double.POSITIVE_INFINITY.roundToInt() == Int.MAX_VALUE}") // true
// Values smaller than Int.MIN_VALUE are rounded to Int.MIN_VALUE
println("(Int.MIN_VALUE.toDouble() - 1.0).roundToInt() == Int.MIN_VALUE is ${(Int.MIN_VALUE.toDouble() - 1.0).roundToInt() == Int.MIN_VALUE}") // true
println("Double.NEGATIVE_INFINITY.roundToInt() == Int.MIN_VALUE is ${Double.NEGATIVE_INFINITY.roundToInt() == Int.MIN_VALUE}") // true
// NaN could not be rounded to Int
// Double.NaN.roundToInt() // will fail with IllegalArgumentException 
   //sampleEnd
}

expect fun Float.roundToInt(): Int(source)

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

Special cases:

  • x.roundToInt() == Int.MAX_VALUE when x > Int.MAX_VALUE

  • x.roundToInt() == Int.MIN_VALUE when x < Int.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.roundToInt()) // 3
println((-10.0f).roundToInt()) // -10
// Values greater than Int.MAX_VALUE are rounded to Int.MAX_VALUE
println("(Int.MAX_VALUE.toFloat() * 2.0f).roundToInt() == Int.MAX_VALUE is ${(Int.MAX_VALUE.toFloat() * 2.0f).roundToInt() == Int.MAX_VALUE}") // true
println("Float.POSITIVE_INFINITY.roundToInt() == Int.MAX_VALUE is ${Float.POSITIVE_INFINITY.roundToInt() == Int.MAX_VALUE}") // true
// Values smaller than Int.MIN_VALUE are rounded to Int.MIN_VALUE
println("(Int.MIN_VALUE.toFloat() * 2.0f).roundToInt() == Int.MIN_VALUE is ${(Int.MIN_VALUE.toFloat() * 2.0f).roundToInt() == Int.MIN_VALUE}") // true
println("Float.NEGATIVE_INFINITY.roundToInt() == Int.MIN_VALUE is ${Float.NEGATIVE_INFINITY.roundToInt() == Int.MIN_VALUE}") // true
// NaN could not be rounded to Int
// Float.NaN.roundToInt() // will fail with IllegalArgumentException 
   //sampleEnd
}
actual fun Double.roundToInt(): Int(source)

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

Special cases:

  • x.roundToInt() == Int.MAX_VALUE when x > Int.MAX_VALUE

  • x.roundToInt() == Int.MIN_VALUE when x < Int.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.roundToInt()) // 3
println((-10.0).roundToInt()) // -10
// Values greater than Int.MAX_VALUE are rounded to Int.MAX_VALUE
println("(Int.MAX_VALUE.toDouble() + 1.0).roundToInt() == Int.MAX_VALUE is ${(Int.MAX_VALUE.toDouble() + 1.0).roundToInt() == Int.MAX_VALUE}") // true
println("Double.POSITIVE_INFINITY.roundToInt() == Int.MAX_VALUE is ${Double.POSITIVE_INFINITY.roundToInt() == Int.MAX_VALUE}") // true
// Values smaller than Int.MIN_VALUE are rounded to Int.MIN_VALUE
println("(Int.MIN_VALUE.toDouble() - 1.0).roundToInt() == Int.MIN_VALUE is ${(Int.MIN_VALUE.toDouble() - 1.0).roundToInt() == Int.MIN_VALUE}") // true
println("Double.NEGATIVE_INFINITY.roundToInt() == Int.MIN_VALUE is ${Double.NEGATIVE_INFINITY.roundToInt() == Int.MIN_VALUE}") // true
// NaN could not be rounded to Int
// Double.NaN.roundToInt() // will fail with IllegalArgumentException 
   //sampleEnd
}

actual inline fun Float.roundToInt(): Int(source)

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

Special cases:

  • x.roundToInt() == Int.MAX_VALUE when x > Int.MAX_VALUE

  • x.roundToInt() == Int.MIN_VALUE when x < Int.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.roundToInt()) // 3
println((-10.0f).roundToInt()) // -10
// Values greater than Int.MAX_VALUE are rounded to Int.MAX_VALUE
println("(Int.MAX_VALUE.toFloat() * 2.0f).roundToInt() == Int.MAX_VALUE is ${(Int.MAX_VALUE.toFloat() * 2.0f).roundToInt() == Int.MAX_VALUE}") // true
println("Float.POSITIVE_INFINITY.roundToInt() == Int.MAX_VALUE is ${Float.POSITIVE_INFINITY.roundToInt() == Int.MAX_VALUE}") // true
// Values smaller than Int.MIN_VALUE are rounded to Int.MIN_VALUE
println("(Int.MIN_VALUE.toFloat() * 2.0f).roundToInt() == Int.MIN_VALUE is ${(Int.MIN_VALUE.toFloat() * 2.0f).roundToInt() == Int.MIN_VALUE}") // true
println("Float.NEGATIVE_INFINITY.roundToInt() == Int.MIN_VALUE is ${Float.NEGATIVE_INFINITY.roundToInt() == Int.MIN_VALUE}") // true
// NaN could not be rounded to Int
// Float.NaN.roundToInt() // will fail with IllegalArgumentException 
   //sampleEnd
}
actual fun Double.roundToInt(): Int(source)

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

Special cases:

  • x.roundToInt() == Int.MAX_VALUE when x > Int.MAX_VALUE

  • x.roundToInt() == Int.MIN_VALUE when x < Int.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.roundToInt()) // 3
println((-10.0).roundToInt()) // -10
// Values greater than Int.MAX_VALUE are rounded to Int.MAX_VALUE
println("(Int.MAX_VALUE.toDouble() + 1.0).roundToInt() == Int.MAX_VALUE is ${(Int.MAX_VALUE.toDouble() + 1.0).roundToInt() == Int.MAX_VALUE}") // true
println("Double.POSITIVE_INFINITY.roundToInt() == Int.MAX_VALUE is ${Double.POSITIVE_INFINITY.roundToInt() == Int.MAX_VALUE}") // true
// Values smaller than Int.MIN_VALUE are rounded to Int.MIN_VALUE
println("(Int.MIN_VALUE.toDouble() - 1.0).roundToInt() == Int.MIN_VALUE is ${(Int.MIN_VALUE.toDouble() - 1.0).roundToInt() == Int.MIN_VALUE}") // true
println("Double.NEGATIVE_INFINITY.roundToInt() == Int.MIN_VALUE is ${Double.NEGATIVE_INFINITY.roundToInt() == Int.MIN_VALUE}") // true
// NaN could not be rounded to Int
// Double.NaN.roundToInt() // will fail with IllegalArgumentException 
   //sampleEnd
}

actual fun Float.roundToInt(): Int(source)

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

Special cases:

  • x.roundToInt() == Int.MAX_VALUE when x > Int.MAX_VALUE

  • x.roundToInt() == Int.MIN_VALUE when x < Int.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.roundToInt()) // 3
println((-10.0f).roundToInt()) // -10
// Values greater than Int.MAX_VALUE are rounded to Int.MAX_VALUE
println("(Int.MAX_VALUE.toFloat() * 2.0f).roundToInt() == Int.MAX_VALUE is ${(Int.MAX_VALUE.toFloat() * 2.0f).roundToInt() == Int.MAX_VALUE}") // true
println("Float.POSITIVE_INFINITY.roundToInt() == Int.MAX_VALUE is ${Float.POSITIVE_INFINITY.roundToInt() == Int.MAX_VALUE}") // true
// Values smaller than Int.MIN_VALUE are rounded to Int.MIN_VALUE
println("(Int.MIN_VALUE.toFloat() * 2.0f).roundToInt() == Int.MIN_VALUE is ${(Int.MIN_VALUE.toFloat() * 2.0f).roundToInt() == Int.MIN_VALUE}") // true
println("Float.NEGATIVE_INFINITY.roundToInt() == Int.MIN_VALUE is ${Float.NEGATIVE_INFINITY.roundToInt() == Int.MIN_VALUE}") // true
// NaN could not be rounded to Int
// Float.NaN.roundToInt() // will fail with IllegalArgumentException 
   //sampleEnd
}
actual fun Double.roundToInt(): Int(source)

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

Special cases:

  • x.roundToInt() == Int.MAX_VALUE when x > Int.MAX_VALUE

  • x.roundToInt() == Int.MIN_VALUE when x < Int.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.roundToInt()) // 3
println((-10.0).roundToInt()) // -10
// Values greater than Int.MAX_VALUE are rounded to Int.MAX_VALUE
println("(Int.MAX_VALUE.toDouble() + 1.0).roundToInt() == Int.MAX_VALUE is ${(Int.MAX_VALUE.toDouble() + 1.0).roundToInt() == Int.MAX_VALUE}") // true
println("Double.POSITIVE_INFINITY.roundToInt() == Int.MAX_VALUE is ${Double.POSITIVE_INFINITY.roundToInt() == Int.MAX_VALUE}") // true
// Values smaller than Int.MIN_VALUE are rounded to Int.MIN_VALUE
println("(Int.MIN_VALUE.toDouble() - 1.0).roundToInt() == Int.MIN_VALUE is ${(Int.MIN_VALUE.toDouble() - 1.0).roundToInt() == Int.MIN_VALUE}") // true
println("Double.NEGATIVE_INFINITY.roundToInt() == Int.MIN_VALUE is ${Double.NEGATIVE_INFINITY.roundToInt() == Int.MIN_VALUE}") // true
// NaN could not be rounded to Int
// Double.NaN.roundToInt() // will fail with IllegalArgumentException 
   //sampleEnd
}

actual fun Float.roundToInt(): Int(source)

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

Special cases:

  • x.roundToInt() == Int.MAX_VALUE when x > Int.MAX_VALUE

  • x.roundToInt() == Int.MIN_VALUE when x < Int.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.roundToInt()) // 3
println((-10.0f).roundToInt()) // -10
// Values greater than Int.MAX_VALUE are rounded to Int.MAX_VALUE
println("(Int.MAX_VALUE.toFloat() * 2.0f).roundToInt() == Int.MAX_VALUE is ${(Int.MAX_VALUE.toFloat() * 2.0f).roundToInt() == Int.MAX_VALUE}") // true
println("Float.POSITIVE_INFINITY.roundToInt() == Int.MAX_VALUE is ${Float.POSITIVE_INFINITY.roundToInt() == Int.MAX_VALUE}") // true
// Values smaller than Int.MIN_VALUE are rounded to Int.MIN_VALUE
println("(Int.MIN_VALUE.toFloat() * 2.0f).roundToInt() == Int.MIN_VALUE is ${(Int.MIN_VALUE.toFloat() * 2.0f).roundToInt() == Int.MIN_VALUE}") // true
println("Float.NEGATIVE_INFINITY.roundToInt() == Int.MIN_VALUE is ${Float.NEGATIVE_INFINITY.roundToInt() == Int.MIN_VALUE}") // true
// NaN could not be rounded to Int
// Float.NaN.roundToInt() // will fail with IllegalArgumentException 
   //sampleEnd
}
actual fun Double.roundToInt(): Int(source)

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

Special cases:

  • x.roundToInt() == Int.MAX_VALUE when x > Int.MAX_VALUE

  • x.roundToInt() == Int.MIN_VALUE when x < Int.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.roundToInt()) // 3
println((-10.0).roundToInt()) // -10
// Values greater than Int.MAX_VALUE are rounded to Int.MAX_VALUE
println("(Int.MAX_VALUE.toDouble() + 1.0).roundToInt() == Int.MAX_VALUE is ${(Int.MAX_VALUE.toDouble() + 1.0).roundToInt() == Int.MAX_VALUE}") // true
println("Double.POSITIVE_INFINITY.roundToInt() == Int.MAX_VALUE is ${Double.POSITIVE_INFINITY.roundToInt() == Int.MAX_VALUE}") // true
// Values smaller than Int.MIN_VALUE are rounded to Int.MIN_VALUE
println("(Int.MIN_VALUE.toDouble() - 1.0).roundToInt() == Int.MIN_VALUE is ${(Int.MIN_VALUE.toDouble() - 1.0).roundToInt() == Int.MIN_VALUE}") // true
println("Double.NEGATIVE_INFINITY.roundToInt() == Int.MIN_VALUE is ${Double.NEGATIVE_INFINITY.roundToInt() == Int.MIN_VALUE}") // true
// NaN could not be rounded to Int
// Double.NaN.roundToInt() // will fail with IllegalArgumentException 
   //sampleEnd
}

actual fun Float.roundToInt(): Int(source)

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

Special cases:

  • x.roundToInt() == Int.MAX_VALUE when x > Int.MAX_VALUE

  • x.roundToInt() == Int.MIN_VALUE when x < Int.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.roundToInt()) // 3
println((-10.0f).roundToInt()) // -10
// Values greater than Int.MAX_VALUE are rounded to Int.MAX_VALUE
println("(Int.MAX_VALUE.toFloat() * 2.0f).roundToInt() == Int.MAX_VALUE is ${(Int.MAX_VALUE.toFloat() * 2.0f).roundToInt() == Int.MAX_VALUE}") // true
println("Float.POSITIVE_INFINITY.roundToInt() == Int.MAX_VALUE is ${Float.POSITIVE_INFINITY.roundToInt() == Int.MAX_VALUE}") // true
// Values smaller than Int.MIN_VALUE are rounded to Int.MIN_VALUE
println("(Int.MIN_VALUE.toFloat() * 2.0f).roundToInt() == Int.MIN_VALUE is ${(Int.MIN_VALUE.toFloat() * 2.0f).roundToInt() == Int.MIN_VALUE}") // true
println("Float.NEGATIVE_INFINITY.roundToInt() == Int.MIN_VALUE is ${Float.NEGATIVE_INFINITY.roundToInt() == Int.MIN_VALUE}") // true
// NaN could not be rounded to Int
// Float.NaN.roundToInt() // will fail with IllegalArgumentException 
   //sampleEnd
}
actual fun Double.roundToInt(): Int(source)

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

Special cases:

  • x.roundToInt() == Int.MAX_VALUE when x > Int.MAX_VALUE

  • x.roundToInt() == Int.MIN_VALUE when x < Int.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.roundToInt()) // 3
println((-10.0).roundToInt()) // -10
// Values greater than Int.MAX_VALUE are rounded to Int.MAX_VALUE
println("(Int.MAX_VALUE.toDouble() + 1.0).roundToInt() == Int.MAX_VALUE is ${(Int.MAX_VALUE.toDouble() + 1.0).roundToInt() == Int.MAX_VALUE}") // true
println("Double.POSITIVE_INFINITY.roundToInt() == Int.MAX_VALUE is ${Double.POSITIVE_INFINITY.roundToInt() == Int.MAX_VALUE}") // true
// Values smaller than Int.MIN_VALUE are rounded to Int.MIN_VALUE
println("(Int.MIN_VALUE.toDouble() - 1.0).roundToInt() == Int.MIN_VALUE is ${(Int.MIN_VALUE.toDouble() - 1.0).roundToInt() == Int.MIN_VALUE}") // true
println("Double.NEGATIVE_INFINITY.roundToInt() == Int.MIN_VALUE is ${Double.NEGATIVE_INFINITY.roundToInt() == Int.MIN_VALUE}") // true
// NaN could not be rounded to Int
// Double.NaN.roundToInt() // will fail with IllegalArgumentException 
   //sampleEnd
}

actual fun Float.roundToInt(): Int(source)

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

Special cases:

  • x.roundToInt() == Int.MAX_VALUE when x > Int.MAX_VALUE

  • x.roundToInt() == Int.MIN_VALUE when x < Int.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.roundToInt()) // 3
println((-10.0f).roundToInt()) // -10
// Values greater than Int.MAX_VALUE are rounded to Int.MAX_VALUE
println("(Int.MAX_VALUE.toFloat() * 2.0f).roundToInt() == Int.MAX_VALUE is ${(Int.MAX_VALUE.toFloat() * 2.0f).roundToInt() == Int.MAX_VALUE}") // true
println("Float.POSITIVE_INFINITY.roundToInt() == Int.MAX_VALUE is ${Float.POSITIVE_INFINITY.roundToInt() == Int.MAX_VALUE}") // true
// Values smaller than Int.MIN_VALUE are rounded to Int.MIN_VALUE
println("(Int.MIN_VALUE.toFloat() * 2.0f).roundToInt() == Int.MIN_VALUE is ${(Int.MIN_VALUE.toFloat() * 2.0f).roundToInt() == Int.MIN_VALUE}") // true
println("Float.NEGATIVE_INFINITY.roundToInt() == Int.MIN_VALUE is ${Float.NEGATIVE_INFINITY.roundToInt() == Int.MIN_VALUE}") // true
// NaN could not be rounded to Int
// Float.NaN.roundToInt() // will fail with IllegalArgumentException 
   //sampleEnd
}