首页 > 解决方案 > 小数到粗俗分数的转换方法 - 从 Swift 转换到 Kotlin 需要帮助

问题描述

我发现了这个漂亮的小数到分数函数:https ://gist.github.com/natecook1000/9ecc976aaac9a035bddf

为了我的应用程序的需要,我已经操纵了上述内容,并希望在制作 Kotlin 版本方面得到帮助。我确实尝试过自己进行转换,但我是一个新手编码期,现在只在 Kotlin 3 周。我没有足够的经验来匹配语言之间的语法和语义来自己完成这一切。帮助表示赞赏:)

我将上面的内容更改为输出 16 英寸的自定义 Unicode 输出(最初不包括 16 英寸)。我需要将其限制为输出的 16、8、4 和 1/2。

如果您不愿意进行整个代码转换,那么可能有助于小数到分数转换器中的特定功能。另外请注意,我已经搜索了很多直接的 Kotlin 版本,但没有找到一个可以像这个一样接受小数输入和分数输出的版本。

主观意见提醒:我喜欢 Swift :) Kotlin 很难学。

我的斯威夫特版本:

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))

到目前为止我的尝试:

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())
}
}

标签: androidkotlindecimalfractions

解决方案


到目前为止我的回答:

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())
}

}

我将一些代码缩小到 kotlin,我无法理解以下代码中的错误和差异:

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 似乎不喜欢嵌套 if 和 else 块中的 return 语句


推荐阅读