1

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

例如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

4

1 回答 1

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);
于 2012-03-09T09:50:25.220 回答