0
我試圖構建一個基本的計算器,並且在輸入期間無法在顯示器上格式化數字。我使用NSNumberFormatter在每3個數字後面加一個逗號,以便「99999」看起來像「99,999」。NSNumberFormatter在UITextField中不允許超過4位數
問題是在輸入過程中它不會顯示超過4位數......它基本上重置爲1位數。這裏是我的輸入數字代碼:
- (IBAction)digitPressed: (UIButton *)sender
{
NSString *digit = sender.titleLabel.text;
display.text = [display.text stringByAppendingString:digit];
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setGroupingSeparator:@","];
[numberFormatter setGroupingSize:3];
[numberFormatter setUsesGroupingSeparator:YES];
[numberFormatter setDecimalSeparator:@"."];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSString *theString = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:[[display text] doubleValue]]];
display.text = theString;
}
當用戶嘗試在小數點後輸入一個零時,您的代碼是否可以處理?爲什麼你會忽略本地化,並以這種方式設置小數點分隔符和分組分隔符? – Sulthan 2013-03-27 11:35:52