0

In order to edit a group-separated number in an UITextField the grouping separator needs to get switched of while editing. Otherwise the returned value (stored as NSNumber value) is (NULL) due to missplaced gouping separators (see the picture and imagine the number is going to be extended by another digit e.g. to "50.0000").

How can I achieve this?

enter image description here

Right now the value is handled in -(void) textFieldDidEndEditing:(UITextField *)textField method.

(My locale is German so the "." is the grouping separator not the decimal separator!) But I try to get the code working for all regions.

Cœur
  • 37,241
  • 25
  • 195
  • 267
JFS
  • 2,992
  • 3
  • 37
  • 48

3 Answers3

1

Try with this solution :

-(void) textFieldDidEndEditing:(UITextField *)textField {

 NSString *stringWithoutDots = [textField.text stringByReplacingOccurrencesOfString:@"." withString:@""]; //remove dots

int number = [stringWithoutDots intValue]; //convert into int

NSNumberFormatter *formatter = [NSNumberFormatter new];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle]; // this line is important!

NSString *formatted = [formatter stringFromNumber:[NSNumber numberWithInteger:number]];



}

I hope this help you. Kevin MACHADO

Kevin Machado
  • 4,141
  • 3
  • 29
  • 54
1

Thanks to thedjnivek and his second suggestion. I had to adapt it a little bit and now it work like a charm. Just for all with the same issue here my final code:

-(void) textFieldDidEndEditing:(UITextField *)textField
{

NSMutableString* mstring = [[textField text] mutableCopy];

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc]init];
[numberFormatter setLocale:[NSLocale currentLocale]];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];

NSString* localeSeparator = [[NSLocale currentLocale]
                             objectForKey:NSLocaleGroupingSeparator];
NSNumber* number = [numberFormatter numberFromString:[mstring
                stringByReplacingOccurrencesOfString:localeSeparator withString:@""]];

    [textField setText:[numberFormatter stringFromNumber:number]];        
}
JFS
  • 2,992
  • 3
  • 37
  • 48
0

You can see here the answer :

How to add commas to number every 3 digits in Objective C?

Don't do your own number formatting. You will almost certainly not get all the edge cases right or correctly handle all possible locales. Use the NSNumberFormatter for formatting numeric data to a localized string representation.

You would use the NSNumberFormatter instance method -setGroupingSeparator: to set the grouping separator to @"." (or better yet [[NSLocale currentLocale] objectForKey:NSLocaleGroupingSeparator]; thanks @ntesler) and -setGroupingSize: to put a grouping separator every 3 digits.

++, Kevin MACHADO

Community
  • 1
  • 1
Kevin Machado
  • 4,141
  • 3
  • 29
  • 54
  • Thanks thedjnivek, I use the `NSNumberFormatter` in order to create the separated output in the text field. The problem occurs when a edited (extended) number in the text field is returned to a (formatted) NSNumber object. If the number is extended by e.g. one digit the returned value is (NULL) due to the missplaces grouping separator. For example: the text field shows "50.000", the user adds a digit at the end to "50.0000", when now returned the text is not returnable as a group separated number object anymore. – JFS Jun 28 '13 at 08:38
  • 1
    Oh ok, sorry, I did not understand :) – Kevin Machado Jun 28 '13 at 08:46