Home > Blockchain >  how to write a formula in Kotlin
how to write a formula in Kotlin

Time:10-22

I'm trying to write a formula where the shorter the distance, the more points you get, the farther the distance, the less points

    fun raiting(distance: Float): Int {
    var rating = 1000
    var maxDistance = 11750F
    
    rating = if (distance > maxDistance) {
        0
    } else{
        ..
    }


    return rating
}

The question is how to write such a formula? Or tell me which formula to use

CodePudding user response:

You can simply calculate percentage of distance from max distance where 0 means 100% and maxDistance means 0%:

fun raiting(distance: Float): Int {
    var maxDistance = 11750F
    
    rating = 100 - (distance * 100 / maxDistance)


    return toInt(rating)
}
  • Related