我想在 '11000' 中添加一个 ',' 像这样的 '11,000' 和十进制 ('.') 在 465 像 4.65。我为 Comma 写了这个:
- (BOOL) textField: (UITextField *)textField shouldChangeCharactersInRange: (NSRange)range replacementString: (NSString *)string {
NSString *unformattedValue;
NSNumberFormatter *formatter;
NSNumber *amount;
switch ([textField tag]) {
case 102:
unformattedValue = [textField.text stringByReplacingOccurrencesOfString:@"," withString:@""];
unformattedValue = [unformattedValue stringByReplacingOccurrencesOfString:@"." withString:@""];
formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setGroupingSeparator:@","];
[formatter setDecimalSeparator:@"."];
amount = [NSNumber numberWithInteger:[unformattedValue intValue]];
textField.text = [formatter stringFromNumber:amount];
break;
default:
break;
}
return YES;
}
而这样做实际上是把像这样的逗号 1,1000 换成 11000。而且我无法做任何接近小数的事情。请帮忙!!