Decimal To Vulgar Fraction Conversion Method- Need Help Converting From Swift To Kotlin
I found this beautiful decimal to fraction function: https://gist.github.com/natecook1000/9ecc976aaac9a035bddf I have manipulated the above for my apps necessity and would like hel
Solution 1:
My answer so far:
classDecimalFraction{
privateval oneSixteenth = "\u00B9" + "/" + "\u2081" + "\u2086"privateval threeSixteenth = "\u00B3" + "/" + "\u2081" + "\u2086"privateval fiveSixteenth = "\u2075" + "/" + "\u2081" + "\u2086"privateval sevenSixteenth = "\u2077" + "/" + "\u2081" + "\u2086"privateval nineSixteenth = "\u2079" + "/" + "\u2081" + "\u2086"privateval elevenSixteenth = "\u00B9" + "\u00B9" + "/" + "\u2081" + "\u2086"privateval thirteenSixteenth = "\u00B9" + "\u00B3" + "/" + "\u2081" + "\u2086"privateval fifteenSixteenth = "\u00B9" + "\u2075" + "/" + "\u2081" + "\u2086"funvulgarFraction(number: Double): Pair<String, Double> {
val fractions = arrayOf(
Pair("", 1),
Pair(this.fifteenSixteenth, 15/16),
Pair("\u215E", 7/8),
Pair(this.thirteenSixteenth, 13/16),
Pair("\u00BE", 3/4),
Pair(this.elevenSixteenth, 11/16),
Pair("\u215D", 5/8),
Pair(this.nineSixteenth, 9/16),
Pair("\u00BD", 1/2),
Pair(this.sevenSixteenth, 7/16),
Pair("\u215C", 3/8),
Pair(this.fiveSixteenth, 5/16),
Pair("\u00BC", 1/4),
Pair(this.threeSixteenth, 3/16),
Pair("\u215B", 1/8),
Pair(this.oneSixteenth, 1/16),
Pair("", 0)
)
val whole = number.toInt()
val sign = if (whole < 0) -1else1val fraction = number - whole.toDouble()
for (i in1..fractions.count()) {
if (abs(fraction) > (fractions[i].1 + fractions[i - 1].1) / 2) {
if (fractions[i - 1].1 == 1.0) {
return ("${whole + sign}", (whole + sign).toDouble())
} else {
return ("$whole" $fractions[i - 1].0, whole.toDouble() + sign.toDouble() * fractions[i - 1].1)
}
}
}
return Pair("$whole", whole.toDouble())
}
}
I got some of the code narrowed to kotlin, I cant understand the errors and differences in the following code:
for (i in1..fractions.count()) {
if (abs(fraction) > (fractions[i].1 + fractions[i - 1].1) / 2) {
if (fractions[i - 1].1 == 1.0) {
return ("${whole + sign}", (whole + sign).toDouble())
} else {
return ("$whole" $fractions[i - 1].0, whole.toDouble() + sign.toDouble() * fractions[i - 1].1)
}
}
}
Kotlin seems to not like the return statements in the nested if and else block
Post a Comment for "Decimal To Vulgar Fraction Conversion Method- Need Help Converting From Swift To Kotlin"