How to add comma and decimals to my UITextField dynamically?

I want to add ',' to '11000', like this one '11, 000 'and decimal ('. ') To 465, for example 4.65. I wrote this for 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; } 

And what it does is, it actually puts a comma like this 1.1000 for 11000. And I can't do anything close to decimal. Please, help!

+1
source share
2 answers

NSNumberFormatter can handle this conversion from string to number and vice versa for you. No need to strip characters with stringByReplacingOccurrencesOfString or use a less soft string for numeric conversion methods like intValue (and at least not use intValue if you want to have a decimal number).

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

Depending on the input you have to endure, you can still do some other cleaning of the input line if the NSNumberFormatter lenient parameter is insufficient. You can also use several number formatters if you want to parse the input string in one format and then output it to another.

+2
source

Use the following code to manually add commas to desired places. You can get the logic from this code and customize it according to your requirements.

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

Source: https://habr.com/ru/post/1208401/


All Articles