Kotlin Program to Round a Number to n Decimal Places

Rounding a number to a specified number of decimal places is a common requirement in programming, especially in financial calculations and data representation. Kotlin provides several ways to achieve this. In this article, we will explore three different Kotlin Program to Round a Number to n Decimal Places. Each method will be explained with an example and its output.

Prerequisites

  • Kotlin Installed: Ensure Kotlin is installed on your system. Download from Kotlin’s official website.
  • IDE: Use an Integrated Development Environment (IDE) like IntelliJ IDEA or Android Studio for running Kotlin programs.
  • Basic Understanding of Kotlin: Familiarity with basic Kotlin syntax and concepts.

1. Using String.format

Program

Kotlin
fun roundToNDecimalPlaces(number: Double, n: Int): String {
    return String.format("%.${n}f", number)
}

fun main() {
    val number = 123.456789
    val n = 3
    val roundedNumber = roundToNDecimalPlaces(number, n)
    println("Original number: $number")
    println("Rounded to $n decimal places: $roundedNumber")
}

Output

Kotlin
Original number: 123.456789
Rounded to 3 decimal places: 123.457

The roundToNDecimalPlaces function uses String.format to round the number 123.456789 to 3 decimal places, resulting in 123.457.

2. Using BigDecimal

Program

Kotlin
import java.math.BigDecimal
import java.math.RoundingMode

fun roundToNDecimalPlacesBigDecimal(number: Double, n: Int): Double {
    val bigDecimal = BigDecimal(number)
    return bigDecimal.setScale(n, RoundingMode.HALF_UP).toDouble()
}

fun main() {
    val number = 123.456789
    val n = 4
    val roundedNumber = roundToNDecimalPlacesBigDecimal(number, n)
    println("Original number: $number")
    println("Rounded to $n decimal places: $roundedNumber")
}

Output

Kotlin
Original number: 123.456789
Rounded to 4 decimal places: 123.4568

The roundToNDecimalPlacesBigDecimal function uses BigDecimal to round the number 123.456789 to 4 decimal places, resulting in 123.4568.

3. Using Kotlin Double Extension Function

Program

Kotlin
fun Double.roundToNDecimalPlaces(n: Int): Double {
    return "%.${n}f".format(this).toDouble()
}

fun main() {
    val number = 123.456789
    val n = 2
    val roundedNumber = number.roundToNDecimalPlaces(n)
    println("Original number: $number")
    println("Rounded to $n decimal places: $roundedNumber")
}

Output

Kotlin
Original number: 123.456789
Rounded to 2 decimal places: 123.46

The roundToNDecimalPlaces extension function for Double rounds the number 123.456789 to 2 decimal places, resulting in 123.46.

Conclusion

Rounding a number to a specified number of decimal places in Kotlin can be done using various methods. String.format is simple and effective for quick formatting. BigDecimal offers precise control and is suitable for financial calculations. Kotlin’s Double extension function provides a convenient and readable way to achieve rounding. Each method serves different needs and can be chosen based on the specific requirements of the application