Vivek Modi
07/30/2021, 9:56 AMprivate fun cmToFeet(value: String): String {
var text = ""
val cm = value.toDouble()
val feet = floor((cm / 2.54) / 12)
val inches = round((cm / 2.54) - (feet * 12))
if (feet > 0 && feet < 10) {
text = "${feet.toInt()}'"
if (inches > 0) {
text += "${inches.toInt()}\""
}
}
return text
}
bsimmons
07/30/2021, 12:39 PMfun cmToFeet(cm: String): String {
val inches = kotlin.math.floor(cm.toDouble() / 2.54).toInt()
val feet = inches / 12
val remainder = inches % 12
fun format(value: Int, suffix: String) = if(value == 0) "" else "$value$suffix"
return format(feet, "'") + format(remainder, "\"")
}
assertEquals("2\"", cmToFeet("6"))
assertEquals("4'2\"", cmToFeet("128"))
assertEquals("4'11\"", cmToFeet("152"))
assertEquals("5'", cmToFeet("152.4"))
Vivek Modi
07/30/2021, 12:42 PM