roundToInt
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
whenx > Int.MAX_VALUE
x.roundToInt() == Int.MIN_VALUE
whenx < Int.MIN_VALUE
Since Kotlin
1.2Throws
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
}
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
whenx > Int.MAX_VALUE
x.roundToInt() == Int.MIN_VALUE
whenx < Int.MIN_VALUE
Since Kotlin
1.2Throws
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
}
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
whenx > Int.MAX_VALUE
x.roundToInt() == Int.MIN_VALUE
whenx < Int.MIN_VALUE
Since Kotlin
1.2Throws
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
}
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
whenx > Int.MAX_VALUE
x.roundToInt() == Int.MIN_VALUE
whenx < Int.MIN_VALUE
Since Kotlin
1.2Throws
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
}
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
whenx > Int.MAX_VALUE
x.roundToInt() == Int.MIN_VALUE
whenx < Int.MIN_VALUE
Since Kotlin
1.2Throws
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
}
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
whenx > Int.MAX_VALUE
x.roundToInt() == Int.MIN_VALUE
whenx < Int.MIN_VALUE
Since Kotlin
1.2Throws
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
}
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
whenx > Int.MAX_VALUE
x.roundToInt() == Int.MIN_VALUE
whenx < Int.MIN_VALUE
Since Kotlin
1.3Throws
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
}
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
whenx > Int.MAX_VALUE
x.roundToInt() == Int.MIN_VALUE
whenx < Int.MIN_VALUE
Since Kotlin
1.3Throws
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
}
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
whenx > Int.MAX_VALUE
x.roundToInt() == Int.MIN_VALUE
whenx < Int.MIN_VALUE
Since Kotlin
1.8Throws
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
}
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
whenx > Int.MAX_VALUE
x.roundToInt() == Int.MIN_VALUE
whenx < Int.MIN_VALUE
Since Kotlin
1.8Throws
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
}
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
whenx > Int.MAX_VALUE
x.roundToInt() == Int.MIN_VALUE
whenx < Int.MIN_VALUE
Since Kotlin
1.8Throws
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
}
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
whenx > Int.MAX_VALUE
x.roundToInt() == Int.MIN_VALUE
whenx < Int.MIN_VALUE
Since Kotlin
1.8Throws
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
}