小数到粗俗分数的转换方法-需要帮助从Swift转换为Kotlin



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

我已经为我的应用程序的必要性操作了上述内容,并希望在制作Kotlin版本时得到帮助。我确实尝试过自己进行转换,但我是一个新手程序员,现在只在科特林工作了3周。我没有足够的经验在语言之间匹配语法和语义来完成这一切。感谢帮助:(

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

如果您不愿意进行完整的代码转换,那么可能有助于小数到小数转换器中的特定函数。另外要注意的是,我已经到处搜索了一个直接的Kotlin版本,但没有找到像这个版本那样采用十进制输入和小数输出的版本。

主观观点提醒:我爱斯威夫特:(科特林很难学。

我的Swift版本:

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

到目前为止我的答案是:

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语句

最新更新