问题
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 help in making a Kotlin version. I have indeed tried making the conversion myself but I am a newbie coder period and have only been at Kotlin 3 weeks now. I don't have enough experience matching syntax and semantics between languages to do it all myself. Help appreciated :)
I changed the above to output custom Unicode outputs for 16th of an inch (16ths were not initially included). I need this to be restricted to 16ths, 8ths, 4ths and 1/2 on output.
If you're not willing to do entire code conversion then possibly help with specific functions within the decimal to fraction converter. Additional note, I have searched high and far for a straight Kotlin version and didn't find one that will take a decimal input and fraction output as this one does.
Subjective opinion alert: I love Swift :) Kotlin is tough to learn.
My Swift version:
func vulgarFraction(number: Double) -> (String, Double) {
let fractions: [(String, Double)] = [("", 1), ("\(fifteenSixteenth)", 15/16), ("\u{215E}", 7/8), ("\(thirteenSixteenth)", 13/16),
("\u{00BE}", 3/4), ("\(elevelSixteenth)", 11/16), ("\u{215D}", 5/8), ("\(nineSixteenth)", 9/16), ("\u{00BD}", 1/2), ("\(sevenSixteenth)", 7/16),
("\u{215C}", 3/8), ("\(fiveSixteenth)", 5/16), ("\u{00BC}", 1/4), ("\(threeSixteenth)", 3/16), ("\u{215B}", 1/8), ("\(oneSixteenth)", 1/16), ("", 0)]
let whole = Int(number)
let sign = whole < 0 ? -1 : 1
let fraction = number - Double(whole)
for i in 1..<fractions.count {
if abs(fraction) > (fractions[i].1 + fractions[i - 1].1) / 2 {
if fractions[i - 1].1 == 1.0 {
return ("\(whole + sign)", Double(whole + sign))
} else {
return ("\(whole) \(fractions[i - 1].0)", Double(whole) + Double(sign) *
fractions[i - 1].1)
}
}
}
return ("\(whole)", Double(whole))
}
let oneSixteenth: String = "\u{00B9}" + "/" + "\u{2081}" + "\u{2086}"
let threeSixteenth: String = "\u{00B3}" + "/" + "\u{2081}" + "\u{2086}"
let fiveSixteenth: String = "\u{2075}" + "/" + "\u{2081}" + "\u{2086}"
let sevenSixteenth: String = "\u{2077}" + "/" + "\u{2081}" + "\u{2086}"
let nineSixteenth: String = "\u{2079}" + "/" + "\u{2081}" + "\u{2086}"
let elevelSixteenth: String = "\u{00B9}" + "\u{00B9}" + "/" + "\u{2081}" + "\u{2086}"
let thirteenSixteenth: String = "\u{00B9}" + "\u{00B3}" + "/" + "\u{2081}" + "\u{2086}"
let fifteenSixteenth: String = "\u{00B9}" + "\u{2075}" + "/" + "\u{2081}" + "\u{2086}"
print(vulgarFraction(number: 4.4375))
My attempt so far:
class DecimalFraction {
private val oneSixteenth = "\u00B9" += "/" += "\u2081" += "\u2086"
private val threeSixteenth = "\u00B3" + "/" + "\u2081" + "\u2086"
private val fiveSixteenth = "\u2075" + "/" + "\u2081" + "\u2086"
private val sevenSixteenth = "\u2077" + "/" + "\u2081" + "\u2086"
private val nineSixteenth = "\u2079" + "/" + "\u2081" + "\u2086"
private val elevenSixteenth = "\u00B9" + "\u00B9" + "/" + "\u2081" + "\u2086"
private val thirteenSixteenth = "\u00B9" + "\u00B3" + "/" + "\u2081" + "\u2086"
private val fifteenSixteenth = "\u00B9" + "\u2075" + "/" + "\u2081" + "\u2086"
fun vulgarFraction(number: Double): Pair<String, Double> {
val fractions = arrayOf(
Pair("", 1),
Pair("$fifteenSixteenth", 15/16),
Pair("\u215E", 7/8),
Pair("$thirteenSixteenth", 13/16),
Pair("\u00BE", 3/4),
Pair("$elevenSixteenth", 11/16),
Pair("\u215D", 5/8),
Pair("$nineSixteenth", 9/16),
Pair("\u00BD", 1/2),
Pair("$sevenSixteenth", 7/16),
Pair("\u215C", 3/8),
Pair("$fiveSixteenth", 5/16),
Pair("\u00BC", 1/4),
Pair("$threeSixteenth", 3/16),
Pair("\u215B", 1/8),
Pair("$oneSixteenth", 1/16),
Pair("", 0)
)
val whole = number.toInt()
val sign = whole < 0 ? -1 : 1 // KOTLIN DOES NOT LIKE THIS LINE
val fraction = number - whole.toDouble()
for (i in 1..fractions.count()) {
if abs(fraction) > (fractions[i].1 + fractions[i - 1].1) / 2 { // KOTLIN DOES NOT LIKE THIS FOR IN LOOP...
if fractions[i - 1].1 == 1.0 { // KOTLIN DOES NOT LIKE THIS FOR IN LOOP...
return ("${whole + sign}", (whole + sign).toDouble) // KOTLIN DOES NOT LIKE THIS FOR IN LOOP...
} else { // KOTLIN DOES NOT LIKE THIS FOR IN LOOP...
return ("$whole" $fractions[i - 1].0, whole.toDouble + sign.toDouble * fractions[i - 1].1) // KOTLIN DOES NOT LIKE THIS FOR IN LOOP...
}
}
}
return Pair("$whole", whole.toDouble())
}
}
回答1:
My answer so far:
class DecimalFraction {
private val oneSixteenth = "\u00B9" + "/" + "\u2081" + "\u2086"
private val threeSixteenth = "\u00B3" + "/" + "\u2081" + "\u2086"
private val fiveSixteenth = "\u2075" + "/" + "\u2081" + "\u2086"
private val sevenSixteenth = "\u2077" + "/" + "\u2081" + "\u2086"
private val nineSixteenth = "\u2079" + "/" + "\u2081" + "\u2086"
private val elevenSixteenth = "\u00B9" + "\u00B9" + "/" + "\u2081" + "\u2086"
private val thirteenSixteenth = "\u00B9" + "\u00B3" + "/" + "\u2081" + "\u2086"
private val fifteenSixteenth = "\u00B9" + "\u2075" + "/" + "\u2081" + "\u2086"
fun vulgarFraction(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) -1 else 1
val fraction = number - whole.toDouble()
for (i in 1..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 in 1..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
来源:https://stackoverflow.com/questions/64532121/decimal-to-vulgar-fraction-conversion-method-need-help-converting-from-swift-to