Swift:如何包含或使用darwin compactdecimalformat?

问题描述 投票:0回答:1

我一直在研究并尝试编写一种紧凑的数字格式,也就是所谓的小规模格式化程序,或者在Swift中将大数字化为较小的数字。

[我突然想到,而不是尝试重新编码轮子,也许Unicode已经解决了这个问题,并且看到确实有一些代码可以压缩更大的数字。

[在Github上Apple Swift的开源代码中,compactdecimalformat.h的引用位于:

https://github.com/apple/swift-corelibs-foundation/blob/master/bootstrap/x86_64-apple-darwin/usr/local/include/unicode/compactdecimalformat.h

其中指的是numberformatter.h;我从Unicode ICU 67.1中找到了一个参考:

https://unicode-org.github.io/icu-docs/apidoc/released/icu4c/numberformatter_8h.html

但是,我不确定如何使用它。我已经导入了达尔文,但不确定如何实际调用compactdecimalformat或公共标识符DecimalFormat

我想对其进行一些数字测试,以查看它是否是我正在寻找的压缩/小数位数。

因此,我的问题是,如何包含和使用作为Darwin导入的一部分附带的compactdecimalnumber.h文件?

谢谢

swift unicode number-formatting darwin
1个回答
0
投票

我认为Swift中没有这样的格式化程序,但是如果需要,您可以继承NumberFormatter并创建自己的CompactNumberFormatter:

class CompactDecimalFormatter: NumberFormatter {
    override func string(for obj: Any?) -> String? {
        guard var value = (obj as? NSNumber)?.doubleValue else { return nil }
        let suffixes = ["", "k", "m", "b","t"]
        var index = suffixes.startIndex
        while index < suffixes.endIndex && value.magnitude >= 1000 {
            index = suffixes.index(after: index)
            value /= 1000
        }
        formatter.positiveSuffix = suffixes[index]
        formatter.negativeSuffix = suffixes[index]
        return formatter.string(for: value)
    }

    private let formatter: NumberFormatter = {
        let formatter = NumberFormatter()

        formatter.numberStyle = .decimal
        formatter.positivePrefix = ""
        formatter.negativePrefix = ""

        formatter.roundingMode = .down
        formatter.minimumFractionDigits = 0
        formatter.maximumFractionDigits = 1
        return formatter
    }()

    // You can override all the properties you thing you might need
    override var minimumIntegerDigits: Int {
        get { formatter.minimumIntegerDigits }
        set { formatter.minimumIntegerDigits = newValue }
    }
    override var maximumIntegerDigits: Int {
        get { formatter.maximumIntegerDigits }
        set { formatter.maximumIntegerDigits = newValue }
    }

    override var minimumFractionDigits: Int {
        get { formatter.minimumFractionDigits }
        set { formatter.minimumFractionDigits = newValue }
    }
    override var maximumFractionDigits: Int {
        get { formatter.maximumFractionDigits }
        set { formatter.maximumFractionDigits = newValue }
    }

    override var positivePrefix: String! {
        get { formatter.positivePrefix }
        set { formatter.positivePrefix = newValue }
    }
    override var negativePrefix: String! {
        get { formatter.negativePrefix }
        set { formatter.negativePrefix = newValue }
    }

    override var roundingMode: RoundingMode {
        get{ formatter.roundingMode }
        set{ formatter.roundingMode = newValue }
    }

    // or disable them
    override var numberStyle: Style { get { .decimal } set { } }
    override var positiveSuffix: String! { get { "" } set { } }
    override var negativeSuffix: String! { get { "" } set { } }
}

let formatter = CompactDecimalFormatter()
formatter.string(for: 2500)  // "2.5k"
© www.soinside.com 2019 - 2024. All rights reserved.