В Kotlin существует несколько способов округления чисел. Вот несколько примеров:
-
Использование Math.ceil():
val number = 3.7 val roundedNumber = Math.ceil(number) println(roundedNumber) // Output: 4.0 -
Использование BigDecimal:
import java.math.BigDecimal import java.math.RoundingMode val number = BigDecimal("3.7") val roundedNumber = number.setScale(0, RoundingMode.CEILING) println(roundedNumber) // Output: 4 -
Использование kotlin.math.ceil():
import kotlin.math.ceil val number = 3.7 val roundedNumber = ceil(number) println(roundedNumber) // Output: 4.0 -
Использование десятичного формата:
import java.text.DecimalFormat val number = 3.7 val decimalFormat = DecimalFormat("#") decimalFormat.roundingMode = DecimalFormat.ROUND_UP val roundedNumber = decimalFormat.format(number) println(roundedNumber) // Output: 4