我正在尝试让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的“添加括号如果太长”选项,但似乎没有一个,并且没有括号的正确显示的第二个标签比第一个更长。
答案 0 :(得分:1)
您可以信任系统在设置格式化程序时做正确的事情,只需强制您想要的语言环境:
NSNumberFormatter *fmtCurrency = [[[NSNumberFormatter alloc] init] autorelease];
[fmtCurrency setNumberStyle: NSNumberFormatterCurrencyStyle];
NSLocale* locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_UK"] autorelease];
[fmtCurrency setLocale:locale];