ios - 在 Swift 中为 Int 添加千位分隔符

标签 ios swift separator

我是 Swift 的新手,在寻找添加空格作为千​​位分隔符的方法时遇到了很多麻烦。

我希望实现的是获取计算结果并将其显示在文本字段中,以便格式为:

2 358 000

代替

2358000

例如。

我不确定是应该格式化 Int 值然后将其转换为 String,还是在 Int 值转换为 String 后添加空格。任何帮助将不胜感激。

最佳答案

您可以使用 NSNumberFormatter 指定不同的分组分隔符,如下所示:

更新:Xcode 11.5 • Swift 5.2

extension Formatter {
    static let withSeparator: NumberFormatter = {
        let formatter = NumberFormatter()
        formatter.numberStyle = .decimal
        formatter.groupingSeparator = " "
        return formatter
    }()
}

extension Numeric {
    var formattedWithSeparator: String { Formatter.withSeparator.string(for: self) ?? "" }
}

2358000.formattedWithSeparator  // "2 358 000"
2358000.99.formattedWithSeparator  // "2 358 000.99"

let int = 2358000
let intFormatted = int.formattedWithSeparator  // "2 358 000"

let decimal: Decimal = 2358000
let decimalFormatted = decimal.formattedWithSeparator  // "2 358 000"

let decimalWithFractionalDigits: Decimal = 2358000.99
let decimalWithFractionalDigitsFormatted = decimalWithFractionalDigits.formattedWithSeparator // "2 358 000.99"

如果您需要在当前语言环境或固定语言环境中将您的值(value)显示为货币:

extension Formatter {
    static let number = NumberFormatter()
}
extension Locale {
    static let englishUS: Locale = .init(identifier: "en_US")
    static let frenchFR: Locale = .init(identifier: "fr_FR")
    static let portugueseBR: Locale = .init(identifier: "pt_BR")
    // ... and so on
}
extension Numeric {
    func formatted(with groupingSeparator: String? = nil, style: NumberFormatter.Style, locale: Locale = .current) -> String {
        Formatter.number.locale = locale
        Formatter.number.numberStyle = style
        if let groupingSeparator = groupingSeparator {
            Formatter.number.groupingSeparator = groupingSeparator
        }
        return Formatter.number.string(for: self) ?? ""
    }
    // Localized
    var currency:   String { formatted(style: .currency) }
    // Fixed locales
    var currencyUS: String { formatted(style: .currency, locale: .englishUS) }
    var currencyFR: String { formatted(style: .currency, locale: .frenchFR) }
    var currencyBR: String { formatted(style: .currency, locale: .portugueseBR) }
    // ... and so on
    var calculator: String { formatted(groupingSeparator: " ", style: .decimal) }
}

用法:

1234.99.currency    // "$1,234.99"

1234.99.currencyUS  // "$1,234.99"
1234.99.currencyFR  // "1 234,99 €"
1234.99.currencyBR  // "R$ 1.234,99"

1234.99.calculator  // "1 234.99"

注意:如果您想要一个与句点宽度相同的空格,您可以使用 "\u{2008}"

unicode spaces

formatter.groupingSeparator = "\u{2008}"

关于ios - 在 Swift 中为 Int 添加千位分隔符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29999024/

相关文章:

ios - 关闭应用程序时如何在后台运行快速应用程序?

ios - 如何在 Swift 中调用 CGRectDivide?

android - 如何移除微调器中的分隔线?

ios - 在 iOS PWA 中从 jsPDF 下载 PDF

objective-c - 如何解决KERN_PROTECTION_FAILURE和KERN_INVALID_ADDRESS?

ios - swift 3 中的委托(delegate)不执行 View 相关代码

c# - ToolStripDropDown 列表的水平分隔符

ios - Apple PushNotification : Need Server Side functionality Guide

ios - Crittercism更新到5.6.2后出现编译错误 "Could not build module ' Crittercism'”

python - 使用Python区分带一个点的线和带两个点的线