Spec-Zone.ru › Kotlin 2

round

kotlin-stdlib/kotlin.math/round

Требования к платформе и версии: Common (1.2), Wasm-JS (1.8), Wasm-WASI (1.8)

expect fun round(x: Double): Double

Округляет заданное значение x до ближайшего целого числа с округлением в случае равного расстояния до ближайшего чётного целого числа.

Особые случаи:

  • round(x) равно x, где x равно NaN или +Inf или -Inf или уже является математическим целым числом.

Примеры

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

Округляет заданное значение x до ближайшего целого числа с округлением в случае равного расстояния до ближайшего чётного целого числа.

Особые случаи:

  • round(x) равно x, где x равно NaN или +Inf или -Inf или уже является математическим целым числом.

Примеры

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
}

Требования к платформе и версии: JS (1.2)

actual fun round(x: Double): Double

Округляет заданное значение x до ближайшего целого числа с округлением в случае равного расстояния до ближайшего чётного целого числа.

Особые случаи:

  • round(x) равно x, где x равно NaN или +Inf или -Inf или уже является математическим целым числом.

Примеры

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

Округляет заданное значение x до ближайшего целого числа с округлением в случае равного расстояния до ближайшего чётного целого числа.

Особые случаи:

  • round(x) равно x, где x равно NaN или +Inf или -Inf или уже является математическим целым числом.

Примеры

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
}

Требования к платформе и версии: JVM (1.2)

actual inline fun round(x: Double): Double

Округляет заданное значение x до ближайшего целого числа с округлением в случае равного расстояния до ближайшего чётного целого числа.

Особые случаи:

  • round(x) равно x, где x равно NaN или +Inf или -Inf или уже является математическим целым числом.

Примеры

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

Округляет заданное значение x до ближайшего целого числа с округлением в случае равного расстояния до ближайшего чётного целого числа.

Особые случаи:

  • round(x) равно x, где x равно NaN или +Inf или -Inf или уже является математическим целым числом.

Примеры

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
}

Требования к платформе и версии: Native (1.3)

actual external fun round(x: Double): Double

Округляет заданное значение x до ближайшего целого числа с округлением в случае равного расстояния до ближайшего чётного целого числа.

Особые случаи:

  • round(x) равно x, где x равно NaN или +Inf или -Inf или уже является математическим целым числом.

Примеры

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

Округляет заданное значение x до ближайшего целого числа с округлением в случае равного расстояния до ближайшего чётного целого числа.

Особые случаи:

  • round(x) равно x, где x равно NaN или +Inf или -Inf или уже является математическим целым числом.

Примеры

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
}

© 2010–2026 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.math/round.html

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API