ios - 如何显示不带0的字符串

标签 ios swift string

我尝试消除字符串末尾的 0。现在我所拥有的是(返回“2101999.0000”)

let point = newObject["PtsBal"] as! String

我用

NSNumberRFormatter()

但是我不知道我哪里做错了。我需要将它作为字符串显示回来。

self.detailArr = ["\((self.userInfo!["FirstName"] as! String).capitalizedString) \((self.userInfo!["LastName"] as! String).capitalizedString)", point, ""]

最佳答案

试试这个

 let point = newObject["PtsBal"] as! String
 let formatter = NSNumberFormatter()
    formatter.numberStyle = .DecimalStyle


    if let number = formatter.numberFromString(point) {
        // number is an instance of NSNumber
        point = String(number.integerValue)

    }

你得到的输出为

enter image description here

最后在需要的地方追加字符串

self.detailArr = ["\((self.userInfo!["FirstName"] as! String).capitalizedString) \((self.userInfo!["LastName"] as! String).capitalizedString)", point, ""]

关于ios - 如何显示不带0的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39388241/

相关文章:

IOS View 仍然没有加载到正确的位置

ios - FIRAnalyticsConnector.framework/FIRAnalyticsConnector' 用于 Mac Catalyst 架构 x86_64 构建

iOS 10.0 UIRefreshControl 不显示指示器

cocoa - 在 Swift 中将存储属性添加到 NSManagedObject 的子类

class - 在 Swift 中,如何遍历对象的类继承?

ios - 添加第二个时,第一个 UIBarButton 消失

ios - 在 Swift 中检查操作系统版本?

java - 如何在文件中搜索字符串

c - 不使用库函数的c中字符串的回文

C++ std::sort 函数没有完成?