NSNumberFormatter文本字段千位分隔符

时间:2012-03-08 22:04:54

标签: objective-c nsnumberformatter

我正在编写信用计算器,我希望当用户输入数字时,使用(本地)千位分隔符来形成数字。

例如100000应该变为100.000(本地)。

我怎么能在我的例子中这样做?

NSNumberFormatter *nf = [[[NSNumberFormatter alloc] init] autorelease];
[nf setNumberStyle: NSNumberFormatterDecimalStyle];
[nf setRoundingMode: NSNumberFormatterRoundHalfUp];
[nf setMaximumFractionDigits: 2];

double loanAmount = [[nf numberFromString: tbxLoanAmount.text] floatValue];
double intRate = [[nf numberFromString: annualInterestRate.text] floatValue];
double NRate = [[nf numberFromString: NInterestRate.text] floatValue];
double years = [[nf numberFromString: noOfYears.text] floatValue];
double residual = [[nf numberFromString: residualvalue.text] floatValue];

非常感谢!

d

1 个答案:

答案 0 :(得分:3)

您可以尝试将以下内容设置为数字格式化程序:

[nf setGroupingSize:3];
[nf setCurrencyGroupingSeparator:@"."];

// you should create a string from number
NSNumber n = [NSNumber numberWithLong: 100000];

NSString str = [nf stringFromNumber: n];

NSLog(@"My number: %@", str);