【发布时间】:2021-05-12 23:46:36
【问题描述】:
这个问题类似于,但没有适用于该问题的答案 - NSNumberFormatter currency symbol
我正在使用 NSNumberFormatter 来格式化货币值,只要货币是用户区域设置的货币,它就可以正常工作。但我还需要显示可能不是用户区域设置的默认货币的货币值(例如,当当前区域设置为 en_US 时显示 GBP,或者当区域设置为 fr_FR 时显示 JPY)。
所以我想使用当前locale设置的规则来显示那些货币,但是设置货币代码后不知道如何获取货币符号。
例如,下面是 NSNumberFormatter 的配置方式:
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.usesGroupingSeparator = YES;
formatter.numberStyle = NSNumberFormatterCurrencyStyle;
formatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US"];
- (lldb) po [currencyFormatter stringFromNumber:[NSNumber numberWithDouble:12.50]]
- 12.50 美元
- (lldb) po currencyFormatter.currencySymbol
- $
- (lldb) po currencyFormatter.currencyCode
- 美元
- (lldb) po currencyFormatter.internationalCurrencySymbol
- 美元
所有这些都是正确的。但是,如果我将货币代码设置为 GBP,如下所示:
currencyFormatter.currencyCode = @"GBP"
我得到以下格式字符串正确且货币代码正确,但货币符号和国际货币符号不正确的地方。
- (lldb) po [currencyFormatter stringFromNumber:[NSNumber numberWithDouble:12.50]]
- 12.50 英镑
- (lldb) po currencyFormatter.currencySymbol
- $ - 错了,应该是 £
- (lldb) po currencyFormatter.currencyCode
- 美元
- (lldb) po currencyFormatter.internationalCurrencySymbol
- USD - 错误,这应该是 GBP
但我真的很奇怪stringFromNumber 方法会返回格式正确的值,带有正确的货币符号,但直接访问 currencySymbol 属性会返回不正确的货币符号。
我需要能够访问货币符号,以便将其缩小并移动其基线,以使货币符号更小并与文本的其余部分偏移。
有谁知道我做错了什么或者设置货币代码后如何获取货币符号?
【问题讨论】:
标签: ios objective-c nsnumberformatter nslocale