Sunday, 1 November 2020

Code explanation (reverse engineer for conversion) Kotlin Android

I have been trying to convert my Swift method to Kotlin and haven't gotten any help here: Decimal to vulgar fraction conversion method- need help converting from Swift to Kotlin

I figured I would change my question a little and see if I can get some help. I want help in reverse engineering/explanation the code so I can make the conversion myself. I would learn more this way anyway.

The full code/story is link above if interested. I'm almost at the point of wanting to pay a freelancer to help with this. Please help :) Additionally you may need to know there is an array with super and subscript outputs for fraction inches that this method rounds to. Need explanation of the following lines:

    val whole = number.toInt() // Of course i know what this does :)
    val sign = if (whole < 0) -1 else 1 // I converted this myself and believe this is correct Kotlin
    val fraction = number - whole.toDouble() // And I know what this does

// need explanation from here down

    for (i in 1..fractions.count()) {
        if (abs(fraction) > (fractions[i].1 + fractions[i - 1].1) / 2) {
            if ((fractions[i - 1].1) == (1.0)) run {
            return@run Pair("${whole + sign}", (whole + sign).toDouble())
        } else {
            return ("$whole" $fractions[i - 1].0, ${whole.toDouble() + (sign.toDouble() * fractions[i - 1].1))
        }
        }
    }


from Code explanation (reverse engineer for conversion) Kotlin Android

No comments:

Post a Comment