错误:未解决的参考:roundToInt

Gop*_*pal 4 kotlin

我正在尝试roundToInt()将双精度值转换为四舍五入的整数,但出现Unresolved Reference异常。

我正在阅读 Kottlin的官方文档,但仍然没有运气。

代码:

编辑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+".")
}
Run Code Online (Sandbox Code Playgroud)

错误日志:

Solution.kt:25:41: error: unresolved reference: roundToInt
var totalCost = (tip+tax+meal_cost).roundToInt()
                                    ^   
Run Code Online (Sandbox Code Playgroud)

pix*_*ix4 6

您需要导入此函数。

import kotlin.math.roundToInt
Run Code Online (Sandbox Code Playgroud)

roundToInt()math包提供的扩展函数,而不是 的成员函数Double