I am trying to use roundToInt()
to convert double value into rounded integer but I am getting Unresolved Reference
exception.
I am going through the official documentation of Kottlin still no luck.
Code:
Edit 1:
fun solve(meal_cost: Double, tip_percent: Int, tax_percent: Int): Unit {
var tip = (meal_cost *tip_percent)/100
var tax = (meal_cost *tax_percent)/100
var totalCost = (tip+tax+meal_cost).roundToInt()
System.out.println("The total cost is "+totalCost+".")
}
Error Log:
Solution.kt:25:41: error: unresolved reference: roundToInt
var totalCost = (tip+tax+meal_cost).roundToInt()
^
Once you sync the Gradle build, the error should disappear. If you still see the unresolved reference error after fixing the problem, try to build your Android application with Command + F9 for Mac or Control + F9 for Windows and Linux. The error should disappear after the build is completed.
To import a package in Kotlin, we use the import keyword, followed by the name of the package that needs to be imported.
ceil() rounds the given value to the nearest integer larger than itself. floor() rounds the given value to the nearest integer smaller than itself. round() rounds the given value towards the nearest integer.
You can use String. format("%. 2f", d) , your double will be rounded automatically.
You need to import this function.
import kotlin.math.roundToInt
roundToInt()
is an extension function provided by the math
package and not a member function of Double
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With