Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: unresolved reference: roundToInt

Tags:

kotlin

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()
                                    ^   
like image 280
Gopal Avatar asked Aug 02 '18 05:08

Gopal


People also ask

How to fix unresolved reference Android studio?

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.

How do I import math to Kotlin?

To import a package in Kotlin, we use the import keyword, followed by the name of the package that needs to be imported.

How do you round down Kotlin?

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.

How do you round double to int in Kotlin?

You can use String. format("%. 2f", d) , your double will be rounded automatically.


1 Answers

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.

like image 81
pixix4 Avatar answered Oct 12 '22 19:10

pixix4