Округление чисел в Котлине: Math.ceil(), BigDecimal, kotlin.math.ceil() и DecimalFormat

В Kotlin существует несколько способов округления чисел. Вот несколько примеров:

  1. Использование Math.ceil():

    val number = 3.7
    val roundedNumber = Math.ceil(number)
    println(roundedNumber) // Output: 4.0
  2. Использование 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
  3. Использование kotlin.math.ceil():

    import kotlin.math.ceil
    val number = 3.7
    val roundedNumber = ceil(number)
    println(roundedNumber) // Output: 4.0
  4. Использование десятичного формата:

    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