【发布时间】:2020-10-26 05:53:23
【问题描述】:
我发现了这个漂亮的小数到分数函数:https://gist.github.com/natecook1000/9ecc976aaac9a035bddf
我已经根据我的应用程序的需要操作了上述内容,并希望在制作 Kotlin 版本方面得到帮助。我确实尝试过自己进行转换,但我是新手程序员,现在只在 Kotlin 3 周。我没有足够的经验来匹配语言之间的语法和语义来自己完成这一切。帮助表示赞赏:)
我将上面的内容更改为输出 16 英寸的自定义 Unicode 输出(最初不包括 16 英寸)。我需要将其限制为 16ths、8ths、4ths 和 1/2 的输出。
如果您不愿意进行整个代码转换,那么可以帮助您使用小数到分数转换器中的特定功能。另外请注意,我已经搜索了很远很远的直接 Kotlin 版本,但没有找到一个可以像这个一样接受小数输入和分数输出的版本。
主观意见提醒:我喜欢 Swift :) 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())
}
}
【问题讨论】:
-
有人愿意提供任何意见吗?我一直在尝试,但还没有走得太远。太多的数学与代码混合在一起,我看不出哪个开始和结束。
标签: android kotlin decimal fractions