android - 小数到粗俗分数的转换方法 - 从 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())
}
}
解决方案
到目前为止我的回答:
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 语句
推荐阅读
- csv - 使用点数据类型属性在 Neo4j 中加载 CSV 文件的 Cypher 查询
- python - 将计数添加到由分组切割产生的 df
- opencart - 如何修改现有的 ocmod.xml 文件而不在 Opencart 2.3 上重新安装新文件
- apache-spark - 在 Spark 2.4 中从 Amazon redshift 读取数据
- javascript - 尝试使用 C# asp.net web 表单使电子纸动态化
- python - 使用列表推导循环
- python - 有没有办法将json中的值从字符串替换为字符串列表
- url - 重定向到另一台服务器时,IIS URL 重写不起作用
- javascript - 选择具有数据名称的所有标签 - 获取值 - 设置类
- c# - Assert.Empty(msgs) 和 Assert.False(msgs.Any()) 有什么区别?