這只是一個指示性的例子,顯示如何做到這一點。你應該研究這些代碼,並將其重新適應你的情況。試試看:
@autoreleasepool
{
// Here I create the formatter and I set the format. You do this faster with setFormat: .
NSNumberFormatter* formatter=[[NSNumberFormatter alloc]init];
formatter.numberStyle= NSNumberFormatterCurrencyStyle;
formatter.maximumIntegerDigits=6;
formatter.maximumFractionDigits=2;
formatter.currencySymbol= @"$";
formatter.currencyDecimalSeparator= @".";
formatter.currencyGroupingSeparator= @",";
[email protected]"";
NSArray* numbers= @[ @1 ,@2 ,@3 ,@4, @5, @6, @7, @8 ];
// These are the inserted numbers, you should change the code in a way that
// every number is taken in input from the text field.
float number=0.0f;
for(NSUInteger i=0;i<8;i++)
{
// It just simulates what happens if the user types the numbers in the array.
number= [numbers[i] floatValue] * 1.0e-2 + number*10;
NSLog(@"%@",[formatter stringFromNumber: @(number)]);
}
}
我想你只格式化字符串蠻力並顯示,每次按下按鈕後更新。 – 2013-02-19 20:07:54
你能解釋一下嗎? – 2013-02-19 20:10:12
NSNumberFormatter可以正常工作(這在很多應用程序中都是常見的)。也許發佈你試圖使用它,並且有人可以指出錯誤。 – 2013-02-19 20:10:55