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.
Properties of basic types
Rounding off a floating-point number to two decimal places
Report a typo
Sample Input 1:
3.14159Sample Output 1:
3.14Sample Input 2:
2.71828Sample Output 2:
2.72Write 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
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.