ios - 在Swift 3中使用.droplast()删除空的小数

标签 ios swift3

我正在尝试删除空的尾随小数,而不是对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/

相关文章:

ios - 动画时将 UITableViewCell 子 UIImageView 置于前面

iphone - keyboardWillShowNotification 边缘案例

ios - TableView 中的 Collectionview - 如何选择项目?

ios - 如何将菜单链接到 ViewController?

ios - 无法使用tvos中的文本字段导出集合将文本字段设置为becomeFirstResponder()

ios - 尝试插入按钮时出现问题? ( swift 用户界面)

ios - 是否可以访问 Facebook 当前的热门话题?

ios - 无法将类型 Promise (_,_) -> DataRequest 的返回表达式转换为返回类型 Promise<DataResponse,AnyObject>>

ios - NSURLSession/NSURLConnection HTTP 加载失败(kCFStreamErrorDomainSSL,-9802)Xamarin.Forms IOS

协议(protocol)扩展内关联类型的快速初始化