Hey I want to convert cm to feet. i create the log...
# android
v
Hey I want to convert cm to feet. i create the logic it's working fine, but problem is if i enter 152 cm it convert into 4'12", i don't want to show like that. instead i want to show 5 foot.
Copy code
private 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
}
b
Not android related, but you probably want something like this?
Copy code
fun 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"))
v
Yeah something like that, thanks