round

expect fun round(x: Double): Double(source)

Rounds the given value x towards the closest integer with ties rounded towards even integer.

Special cases:

  • round(x) is x where x is NaN or +Inf or -Inf or already a mathematical integer.

Since Kotlin

1.2

Samples

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

fun main() { 
   //sampleStart 
   println(round(3.49)) // 3.0
println(round(3.51)) // 4.0
// 3.5 is between 3.0 and 4.0, so it is rounded towards an even number 4.0
println(round(3.5)) // 4.0
// 2.5 is between 2.0 and 3.0, so it is rounded towards an even number 2.0
println(round(2.5)) // 2.0
// -10.0 is already an "integer", so no rounding will take place
println(truncate(-10.0)) // -10.0
// Special cases
println(round(Double.NaN)) // NaN
println(round(Double.POSITIVE_INFINITY)) // Infinity
println(round(Double.NEGATIVE_INFINITY)) // -Infinity 
   //sampleEnd
}
import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   println(floor(3.5)) // 3.0
println(ceil(3.5)) // 4.0
println(truncate(3.5)) // 3.0
println(round(3.5)) // 4.0
println(round(3.49)) // 3.0

println(floor(-3.5)) // -4.0
println(ceil(-3.5)) // -3.0
println(truncate(-3.5)) // -3.0
println(round(-3.5)) // -4.0
println(round(-3.49)) // -3.0 
   //sampleEnd
}

expect fun round(x: Float): Float(source)

Rounds the given value x towards the closest integer with ties rounded towards even integer.

Special cases:

  • round(x) is x where x is NaN or +Inf or -Inf or already a mathematical integer.

Since Kotlin

1.2

Samples

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

fun main() { 
   //sampleStart 
   println(round(3.49f)) // 3.0
println(round(3.51f)) // 4.0
// 3.5 is between 3.0 and 4.0, so it is rounded towards an even number 4.0
println(round(3.5f)) // 4.0
// 2.5 is between 2.0 and 3.0, so it is rounded towards an even number 2.0
println(round(2.5f)) // 2.0
// -10.0 is already an "integer", so no rounding will take place
println(truncate(-10.0f)) // -10.0
// Special cases
println(round(Float.NaN)) // NaN
println(round(Float.POSITIVE_INFINITY)) // Infinity
println(round(Float.NEGATIVE_INFINITY)) // -Infinity 
   //sampleEnd
}
import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   println(floor(3.5f)) // 3.0
println(ceil(3.5f)) // 4.0
println(truncate(3.5f)) // 3.0
println(round(3.5f)) // 4.0
println(round(3.49f)) // 3.0

println(floor(-3.5f)) // -4.0
println(ceil(-3.5f)) // -3.0
println(truncate(-3.5f)) // -3.0
println(round(-3.5f)) // -4.0
println(round(-3.49f)) // -3.0 
   //sampleEnd
}
actual fun round(x: Double): Double(source)

Rounds the given value x towards the closest integer with ties rounded towards even integer.

Special cases:

  • round(x) is x where x is NaN or +Inf or -Inf or already a mathematical integer.

Since Kotlin

1.2

Samples

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

fun main() { 
   //sampleStart 
   println(round(3.49)) // 3.0
println(round(3.51)) // 4.0
// 3.5 is between 3.0 and 4.0, so it is rounded towards an even number 4.0
println(round(3.5)) // 4.0
// 2.5 is between 2.0 and 3.0, so it is rounded towards an even number 2.0
println(round(2.5)) // 2.0
// -10.0 is already an "integer", so no rounding will take place
println(truncate(-10.0)) // -10.0
// Special cases
println(round(Double.NaN)) // NaN
println(round(Double.POSITIVE_INFINITY)) // Infinity
println(round(Double.NEGATIVE_INFINITY)) // -Infinity 
   //sampleEnd
}
import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   println(floor(3.5)) // 3.0
println(ceil(3.5)) // 4.0
println(truncate(3.5)) // 3.0
println(round(3.5)) // 4.0
println(round(3.49)) // 3.0

println(floor(-3.5)) // -4.0
println(ceil(-3.5)) // -3.0
println(truncate(-3.5)) // -3.0
println(round(-3.5)) // -4.0
println(round(-3.49)) // -3.0 
   //sampleEnd
}

actual inline fun round(x: Float): Float(source)

Rounds the given value x towards the closest integer with ties rounded towards even integer.

Special cases:

  • round(x) is x where x is NaN or +Inf or -Inf or already a mathematical integer.

Since Kotlin

1.2

Samples

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

fun main() { 
   //sampleStart 
   println(round(3.49f)) // 3.0
println(round(3.51f)) // 4.0
// 3.5 is between 3.0 and 4.0, so it is rounded towards an even number 4.0
println(round(3.5f)) // 4.0
// 2.5 is between 2.0 and 3.0, so it is rounded towards an even number 2.0
println(round(2.5f)) // 2.0
// -10.0 is already an "integer", so no rounding will take place
println(truncate(-10.0f)) // -10.0
// Special cases
println(round(Float.NaN)) // NaN
println(round(Float.POSITIVE_INFINITY)) // Infinity
println(round(Float.NEGATIVE_INFINITY)) // -Infinity 
   //sampleEnd
}
import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   println(floor(3.5f)) // 3.0
println(ceil(3.5f)) // 4.0
println(truncate(3.5f)) // 3.0
println(round(3.5f)) // 4.0
println(round(3.49f)) // 3.0

println(floor(-3.5f)) // -4.0
println(ceil(-3.5f)) // -3.0
println(truncate(-3.5f)) // -3.0
println(round(-3.5f)) // -4.0
println(round(-3.49f)) // -3.0 
   //sampleEnd
}
actual inline fun round(x: Double): Double(source)

Rounds the given value x towards the closest integer with ties rounded towards even integer.

Special cases:

  • round(x) is x where x is NaN or +Inf or -Inf or already a mathematical integer.

Since Kotlin

1.2

Samples

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

fun main() { 
   //sampleStart 
   println(round(3.49)) // 3.0
println(round(3.51)) // 4.0
// 3.5 is between 3.0 and 4.0, so it is rounded towards an even number 4.0
println(round(3.5)) // 4.0
// 2.5 is between 2.0 and 3.0, so it is rounded towards an even number 2.0
println(round(2.5)) // 2.0
// -10.0 is already an "integer", so no rounding will take place
println(truncate(-10.0)) // -10.0
// Special cases
println(round(Double.NaN)) // NaN
println(round(Double.POSITIVE_INFINITY)) // Infinity
println(round(Double.NEGATIVE_INFINITY)) // -Infinity 
   //sampleEnd
}
import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   println(floor(3.5)) // 3.0
println(ceil(3.5)) // 4.0
println(truncate(3.5)) // 3.0
println(round(3.5)) // 4.0
println(round(3.49)) // 3.0

println(floor(-3.5)) // -4.0
println(ceil(-3.5)) // -3.0
println(truncate(-3.5)) // -3.0
println(round(-3.5)) // -4.0
println(round(-3.49)) // -3.0 
   //sampleEnd
}

actual inline fun round(x: Float): Float(source)

Rounds the given value x towards the closest integer with ties rounded towards even integer.

Special cases:

  • round(x) is x where x is NaN or +Inf or -Inf or already a mathematical integer.

Since Kotlin

1.2

Samples

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

fun main() { 
   //sampleStart 
   println(round(3.49f)) // 3.0
println(round(3.51f)) // 4.0
// 3.5 is between 3.0 and 4.0, so it is rounded towards an even number 4.0
println(round(3.5f)) // 4.0
// 2.5 is between 2.0 and 3.0, so it is rounded towards an even number 2.0
println(round(2.5f)) // 2.0
// -10.0 is already an "integer", so no rounding will take place
println(truncate(-10.0f)) // -10.0
// Special cases
println(round(Float.NaN)) // NaN
println(round(Float.POSITIVE_INFINITY)) // Infinity
println(round(Float.NEGATIVE_INFINITY)) // -Infinity 
   //sampleEnd
}
import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   println(floor(3.5f)) // 3.0
println(ceil(3.5f)) // 4.0
println(truncate(3.5f)) // 3.0
println(round(3.5f)) // 4.0
println(round(3.49f)) // 3.0

println(floor(-3.5f)) // -4.0
println(ceil(-3.5f)) // -3.0
println(truncate(-3.5f)) // -3.0
println(round(-3.5f)) // -4.0
println(round(-3.49f)) // -3.0 
   //sampleEnd
}
actual external fun round(x: Double): Double(source)

Rounds the given value x towards the closest integer with ties rounded towards even integer.

Special cases:

  • round(x) is x where x is NaN or +Inf or -Inf or already a mathematical integer.

Since Kotlin

1.3

Samples

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

fun main() { 
   //sampleStart 
   println(round(3.49)) // 3.0
println(round(3.51)) // 4.0
// 3.5 is between 3.0 and 4.0, so it is rounded towards an even number 4.0
println(round(3.5)) // 4.0
// 2.5 is between 2.0 and 3.0, so it is rounded towards an even number 2.0
println(round(2.5)) // 2.0
// -10.0 is already an "integer", so no rounding will take place
println(truncate(-10.0)) // -10.0
// Special cases
println(round(Double.NaN)) // NaN
println(round(Double.POSITIVE_INFINITY)) // Infinity
println(round(Double.NEGATIVE_INFINITY)) // -Infinity 
   //sampleEnd
}
import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   println(floor(3.5)) // 3.0
println(ceil(3.5)) // 4.0
println(truncate(3.5)) // 3.0
println(round(3.5)) // 4.0
println(round(3.49)) // 3.0

println(floor(-3.5)) // -4.0
println(ceil(-3.5)) // -3.0
println(truncate(-3.5)) // -3.0
println(round(-3.5)) // -4.0
println(round(-3.49)) // -3.0 
   //sampleEnd
}

actual external fun round(x: Float): Float(source)

Rounds the given value x towards the closest integer with ties rounded towards even integer.

Special cases:

  • round(x) is x where x is NaN or +Inf or -Inf or already a mathematical integer.

Since Kotlin

1.3

Samples

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

fun main() { 
   //sampleStart 
   println(round(3.49f)) // 3.0
println(round(3.51f)) // 4.0
// 3.5 is between 3.0 and 4.0, so it is rounded towards an even number 4.0
println(round(3.5f)) // 4.0
// 2.5 is between 2.0 and 3.0, so it is rounded towards an even number 2.0
println(round(2.5f)) // 2.0
// -10.0 is already an "integer", so no rounding will take place
println(truncate(-10.0f)) // -10.0
// Special cases
println(round(Float.NaN)) // NaN
println(round(Float.POSITIVE_INFINITY)) // Infinity
println(round(Float.NEGATIVE_INFINITY)) // -Infinity 
   //sampleEnd
}
import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   println(floor(3.5f)) // 3.0
println(ceil(3.5f)) // 4.0
println(truncate(3.5f)) // 3.0
println(round(3.5f)) // 4.0
println(round(3.49f)) // 3.0

println(floor(-3.5f)) // -4.0
println(ceil(-3.5f)) // -3.0
println(truncate(-3.5f)) // -3.0
println(round(-3.5f)) // -4.0
println(round(-3.49f)) // -3.0 
   //sampleEnd
}
actual fun round(x: Double): Double(source)

Rounds the given value x towards the closest integer with ties rounded towards even integer.

Special cases:

  • round(x) is x where x is NaN or +Inf or -Inf or already a mathematical integer.

Since Kotlin

1.8

Samples

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

fun main() { 
   //sampleStart 
   println(round(3.49)) // 3.0
println(round(3.51)) // 4.0
// 3.5 is between 3.0 and 4.0, so it is rounded towards an even number 4.0
println(round(3.5)) // 4.0
// 2.5 is between 2.0 and 3.0, so it is rounded towards an even number 2.0
println(round(2.5)) // 2.0
// -10.0 is already an "integer", so no rounding will take place
println(truncate(-10.0)) // -10.0
// Special cases
println(round(Double.NaN)) // NaN
println(round(Double.POSITIVE_INFINITY)) // Infinity
println(round(Double.NEGATIVE_INFINITY)) // -Infinity 
   //sampleEnd
}
import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   println(floor(3.5)) // 3.0
println(ceil(3.5)) // 4.0
println(truncate(3.5)) // 3.0
println(round(3.5)) // 4.0
println(round(3.49)) // 3.0

println(floor(-3.5)) // -4.0
println(ceil(-3.5)) // -3.0
println(truncate(-3.5)) // -3.0
println(round(-3.5)) // -4.0
println(round(-3.49)) // -3.0 
   //sampleEnd
}

actual fun round(x: Float): Float(source)

Rounds the given value x towards the closest integer with ties rounded towards even integer.

Special cases:

  • round(x) is x where x is NaN or +Inf or -Inf or already a mathematical integer.

Since Kotlin

1.8

Samples

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

fun main() { 
   //sampleStart 
   println(round(3.49f)) // 3.0
println(round(3.51f)) // 4.0
// 3.5 is between 3.0 and 4.0, so it is rounded towards an even number 4.0
println(round(3.5f)) // 4.0
// 2.5 is between 2.0 and 3.0, so it is rounded towards an even number 2.0
println(round(2.5f)) // 2.0
// -10.0 is already an "integer", so no rounding will take place
println(truncate(-10.0f)) // -10.0
// Special cases
println(round(Float.NaN)) // NaN
println(round(Float.POSITIVE_INFINITY)) // Infinity
println(round(Float.NEGATIVE_INFINITY)) // -Infinity 
   //sampleEnd
}
import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   println(floor(3.5f)) // 3.0
println(ceil(3.5f)) // 4.0
println(truncate(3.5f)) // 3.0
println(round(3.5f)) // 4.0
println(round(3.49f)) // 3.0

println(floor(-3.5f)) // -4.0
println(ceil(-3.5f)) // -3.0
println(truncate(-3.5f)) // -3.0
println(round(-3.5f)) // -4.0
println(round(-3.49f)) // -3.0 
   //sampleEnd
}
actual fun round(x: Double): Double(source)

Rounds the given value x towards the closest integer with ties rounded towards even integer.

Special cases:

  • round(x) is x where x is NaN or +Inf or -Inf or already a mathematical integer.

Since Kotlin

1.8

Samples

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

fun main() { 
   //sampleStart 
   println(round(3.49)) // 3.0
println(round(3.51)) // 4.0
// 3.5 is between 3.0 and 4.0, so it is rounded towards an even number 4.0
println(round(3.5)) // 4.0
// 2.5 is between 2.0 and 3.0, so it is rounded towards an even number 2.0
println(round(2.5)) // 2.0
// -10.0 is already an "integer", so no rounding will take place
println(truncate(-10.0)) // -10.0
// Special cases
println(round(Double.NaN)) // NaN
println(round(Double.POSITIVE_INFINITY)) // Infinity
println(round(Double.NEGATIVE_INFINITY)) // -Infinity 
   //sampleEnd
}
import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   println(floor(3.5)) // 3.0
println(ceil(3.5)) // 4.0
println(truncate(3.5)) // 3.0
println(round(3.5)) // 4.0
println(round(3.49)) // 3.0

println(floor(-3.5)) // -4.0
println(ceil(-3.5)) // -3.0
println(truncate(-3.5)) // -3.0
println(round(-3.5)) // -4.0
println(round(-3.49)) // -3.0 
   //sampleEnd
}

actual fun round(x: Float): Float(source)

Rounds the given value x towards the closest integer with ties rounded towards even integer.

Special cases:

  • round(x) is x where x is NaN or +Inf or -Inf or already a mathematical integer.

Since Kotlin

1.8

Samples

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

fun main() { 
   //sampleStart 
   println(round(3.49f)) // 3.0
println(round(3.51f)) // 4.0
// 3.5 is between 3.0 and 4.0, so it is rounded towards an even number 4.0
println(round(3.5f)) // 4.0
// 2.5 is between 2.0 and 3.0, so it is rounded towards an even number 2.0
println(round(2.5f)) // 2.0
// -10.0 is already an "integer", so no rounding will take place
println(truncate(-10.0f)) // -10.0
// Special cases
println(round(Float.NaN)) // NaN
println(round(Float.POSITIVE_INFINITY)) // Infinity
println(round(Float.NEGATIVE_INFINITY)) // -Infinity 
   //sampleEnd
}
import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   println(floor(3.5f)) // 3.0
println(ceil(3.5f)) // 4.0
println(truncate(3.5f)) // 3.0
println(round(3.5f)) // 4.0
println(round(3.49f)) // 3.0

println(floor(-3.5f)) // -4.0
println(ceil(-3.5f)) // -3.0
println(truncate(-3.5f)) // -3.0
println(round(-3.5f)) // -4.0
println(round(-3.49f)) // -3.0 
   //sampleEnd
}