Rounding off a floating-point number to two decimal places

Report a typo

Write a program in Kotlin that takes a floating point number as input. The program should then round off that number to two decimal places. If the input is not a floating point number, it should output an error message.

Sample Input 1:

3.14159

Sample Output 1:

3.14

Sample Input 2:

2.71828

Sample Output 2:

2.72
Write a program in Kotlin
import java.util.Scanner

fun main(args: Array<String>) {
// Creating a Scanner object for reading input
val read = Scanner(System.`in`)

// User input is read and stored in a variable
val userInput = read.nextLine()

// Check if user input is a floating point number
try {
val floatNumber = userInput.toFloat()

// TODO: Round off the floating point number to two decimal places

} catch (e: NumberFormatException) {
// Output an error message if the input cannot be converted to a float
println("Input is not a floating-point number")
}
}

Create a free account to access the full topic