我正在尝试将结果从公里转换为英里,但我得到的结果并不正确。
这里似乎有什么问题?
when {
// TIME: To calculate your time, fill in your distance and pace
time == null -> {
val calculatedTime = distance!!.toLong() * timeToSeconds(pace.toString())
result.text = "The runner's time is ${secondsToTime(calculatedTime)}"
}
// DISTANCE: To calculate your distance, fill in your time and pace
distance == null -> {
val calculatedDistance = ((timeToSeconds(time).div(timeToSeconds
(pace.toString()))) * 0.621371).format(2)
result.text = "Distance is $calculatedDistance Miles"
}
// PACE: To calculate your pace, fill in your time and distance
pace == null -> {
// Calculate Pace
val calculatedPace: Long = timeToSeconds(time).toLong() / distance.toLong()
Log.i("PaceSeconds", calculatedPace.toString() +
secondsToTime(calculatedPace))
result.text = "The runner's pace in miles is ${secondsToTime(calculatedPace)}"
}
}
答案 0 :(得分:0)
这是一个四舍五入的问题。让我们分解一下:
val timeInSeconds = timeToSeconds(time)
val paceInSeconds = timeToSeconds(pace)
val timeDivPace = timeInSeconds.div(paceInSeconds)
val calculatedDistance = timeDivPace * 0.621371
使用评论中的示例数据,timeInSeconds
为612,paceInSeconds
为83.您希望timeDivPace
为7.373494,但实际上为7,因为两个整数的除法返回结果为整数。
修复很简单:将被除数或除数转换为浮点数,例如:
val timeDivPace = timeInSeconds.div(paceInSeconds.toFloat())