我正在尝试删除空的尾随小数,而不是对float使用扩展名,如果结果有任何尾随零,我想考虑使用droplast():
if result.contains(".0") {
result.characters.dropLast()
}
if result.contains(".00") {
result.characters.dropLast(2)
}
这似乎不起作用,我得到警告:
调用'droplast()'的结果未使用
最佳答案
使用正则表达式的替代解决方案,小数位数无关紧要:
var result = "1.00"
result = result.replacingOccurrences(of: "\\.0+$",
with: "",
options: .regularExpression,
range: result.startIndex..<result.endIndex)
考虑到整个字符串,您甚至可以忽略
range
参数:result = result.replacingOccurrences(of: "\\.0+$",
with: "",
options: .regularExpression)
关于ios - 在Swift 3中使用.droplast()删除空的小数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39785917/