如何将逗号和小数位dynamic地放入我的UITextField中?

我想在像4.65这样的“11000”和465中的小数点('。')中添加一个','。 我写了这个逗号:

- (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; } 

而这样做实际上是把11000这样的逗号作为11000.而且我不能做任何十进制closures。 请帮忙!!

NSNumberFormatter可以处理这个从string到数字的转换,并返回给你。 不需要使用stringByReplacingOccurrencesOfString自己stringByReplacingOccurrencesOfString字符, stringByReplacingOccurrencesOfString需要像intValue那样使用宽松的string到数字转换方法(并且如果想要得到小数,至less不要使用intValue )。

 NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init]; [formatter setNumberStyle:NSNumberFormatterDecimalStyle]; NSNumber *amount = [formatter numberFromString:textField.text]; textField.text = [formatter stringFromNumber:amount]; 

根据您需要忍受的input,如果NSNumberFormatterlenient设置不够,可能仍然需要对inputstring进行一些其他的清理。 如果您想以一种格式parsinginputstring,然后在另一个格式中输出,也可以使用多个数字格式器。

使用以下代码在正确的位置手动添加逗号。 你可以从这个代码中得到逻辑,并调整它以适应你的需求。

 -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { NSLog(@"Text:%@textField length:%dRange.length:%lu , Range.location:%lu :: replacementString:%@",textField.text,textField.text.length,(unsigned long)range.length,(unsigned long)range.location,string); NSMutableString *tempString=textField.text.mutableCopy; int digitsCount; if ([string isEqualToString:@""]) //digit removed { NSLog(@"digit removed, string length after trimming:%d",[tempString stringByReplacingOccurrencesOfString:@"," withString:@""].length); digitsCount=[tempString stringByReplacingOccurrencesOfString:@"," withString:@""].length-1; //digit removed }else ///digit added { digitsCount=[tempString stringByReplacingOccurrencesOfString:@"," withString:@""].length+1 ; } NSLog(@"Number of digits:%d",digitsCount); switch (digitsCount) { //case 1:textField.text=[tempString stringByReplacingOccurrencesOfString:@"," withString:@""]; //break; case 3:textField.text=[tempString stringByReplacingOccurrencesOfString:@"," withString:@""]; break; case 4: //remove previous comma... tempString=[tempString stringByReplacingOccurrencesOfString:@"," withString:@""].mutableCopy; [tempString insertString:@"," atIndex:1]; textField.text=tempString; break; case 5: //remove previous comma... tempString=[tempString stringByReplacingOccurrencesOfString:@"," withString:@""].mutableCopy; [tempString insertString:@"," atIndex:2]; textField.text=tempString; break; case 6: //remove previous comma... tempString=[tempString stringByReplacingOccurrencesOfString:@"," withString:@""].mutableCopy; [tempString insertString:@"," atIndex:1]; [tempString insertString:@"," atIndex:4]; textField.text=tempString; break; default: break; } return YES; }