我正在尝试获取NSNumberFormatter以显示货币。我有两个UILabel,都由相同的NSNumberFormatter格式化,但是第一个被方括号包围。这是我的代码:
NSNumberFormatter *fmtCurrency = [[[NSNumberFormatter alloc] init] autorelease];
[fmtCurrency setNumberStyle: NSNumberFormatterCurrencyStyle];
[fmtCurrency setGeneratesDecimalNumbers:TRUE];
[fmtCurrency setCurrencyCode:@"GBP"];
[fmtCurrency setCurrencySymbol:@"£"];
// formatter generates decimal numbers
txtTotal.text = [fmtCurrency stringFromNumber: result.totalBill ]; // displays (£1100.00)
txtTransition.text = [fmtCurrency stringFromNumber: result.transition]; // displays £120,000.00
我在InterfaceBuilder周围寻找了“如果太长则加括号”选项,但似乎没有,并且没有括号正确显示的第二个标签比第一个长。
最佳答案
您可以相信系统在设置格式化程序时可以做正确的事情,只需强制设置所需的语言环境即可:
NSNumberFormatter *fmtCurrency = [[[NSNumberFormatter alloc] init] autorelease];
[fmtCurrency setNumberStyle: NSNumberFormatterCurrencyStyle];
NSLocale* locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_UK"] autorelease];
[fmtCurrency setLocale:locale];