使用 NSNumberFormatter 在货币符号和值之间填充空格

发布于 2024-08-23 10:43:35 字数 290 浏览 9 评论 0原文

如果这是一个愚蠢的问题,我深表歉意,但我正在尝试为我的 iPhone 应用程序格式化货币值,并且正在努力左对齐货币符号,但右对齐该值。因此,“$123.45”的格式为(例如)

$   123.45
depending on format-width. This is a kind of accounting format (I think).

我已经尝试了 NSNumberFormatter 的各种方法,但无法得到我需要的。

谁能建议如何做到这一点?

谢谢菲托

Apologies if this is a dumb question but I'm trying to format a currency value for my iphone app and am struggling to left-justify the currency symbol, but right-justify the value. So, "$123.45" is formatted as (say)

$   123.45

depending on format-width. This is a kind of accounting format (I think).

I've tried various methods with NSNumberFormatter but can't get what I need.

Can anyone advise on how to do this?

Thanks

Fitto

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月竹挽风 2024-08-30 10:43:35

您正在寻找 NSNumberFormatterpaddingPosition 属性。您需要将其设置为 NSNumberFormatterPadAfterPrefix 以获得所需的格式。

You're looking for the paddingPosition property of NSNumberFormatter. You need to set this to NSNumberFormatterPadAfterPrefix for the desired format.

两个我 2024-08-30 10:43:35

这对我不起作用。通过这样做,我可以在货币符号和金额之间添加空格。

Swift 3.0

currencyFormatter.negativePrefix = "\(currencyFormatter.negativePrefix!) "
currencyFormatter.positivePrefix = "\(currencyFormatter.positivePrefix!) "

完整代码:

extension Int {
    func amountStringInCurrency(currencyCode: String) -> (str: String, nr: Double) {
        let currencyFormatter = NumberFormatter()
        currencyFormatter.usesGroupingSeparator = true
        currencyFormatter.numberStyle = .currency
        currencyFormatter.currencyCode = currencyCode
        currencyFormatter.negativePrefix = "\(currencyFormatter.negativePrefix!) "
        currencyFormatter.positivePrefix = "\(currencyFormatter.positivePrefix!) "

        let nrOfDigits = currencyFormatter.maximumFractionDigits
        let number: Double = Double(self)/pow(10, Double(nrOfDigits))
        return (currencyFormatter.string(from: NSNumber(value: number))!, number)
    }
}

此扩展位于以 MinorUnits 表示金额的 Int 上。即美元用2位数字表示,而日元则不用数字表示。这就是此扩展将返回的内容:

let amountInMinorUnits: Int = 1234
amountInMinorUnits.amountStringInCurrency(currencyCode: "USD").str // $ 12.34
amountInMinorUnits.amountStringInCurrency(currencyCode: "JPY").str // JP¥ 1,234

千位和小数分隔符由用户区域设置确定。

This didn't work for me. I was able to add a space between the currency symbol and the amount by doing this.

Swift 3.0

currencyFormatter.negativePrefix = "\(currencyFormatter.negativePrefix!) "
currencyFormatter.positivePrefix = "\(currencyFormatter.positivePrefix!) "

Complete code:

extension Int {
    func amountStringInCurrency(currencyCode: String) -> (str: String, nr: Double) {
        let currencyFormatter = NumberFormatter()
        currencyFormatter.usesGroupingSeparator = true
        currencyFormatter.numberStyle = .currency
        currencyFormatter.currencyCode = currencyCode
        currencyFormatter.negativePrefix = "\(currencyFormatter.negativePrefix!) "
        currencyFormatter.positivePrefix = "\(currencyFormatter.positivePrefix!) "

        let nrOfDigits = currencyFormatter.maximumFractionDigits
        let number: Double = Double(self)/pow(10, Double(nrOfDigits))
        return (currencyFormatter.string(from: NSNumber(value: number))!, number)
    }
}

This extension is on an Int that expresses the amount in MinorUnits. I.e. USD is expressed with 2 digits, while japanese yen is expressed without digits. So this is what this extension would return:

let amountInMinorUnits: Int = 1234
amountInMinorUnits.amountStringInCurrency(currencyCode: "USD").str // $ 12.34
amountInMinorUnits.amountStringInCurrency(currencyCode: "JPY").str // JP¥ 1,234

The thousand and decimal separator are determined by the users locale.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文