0

How to add commas for every 3 numbers while user inputting the text field and also display the number with commas on the label? I am using this function but it does not work.

- (void)commaFormatter
{
  NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
  [numberFormatter setGroupingSeparator:@","];
  [numberFormatter setGroupingSize:3];
  [numberFormatter setDecimalSeparator:@"."];
  [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
}
Winter
  • 47
  • 1
  • 6
  • 1
    You should not be hardcoding any of the grouping attributes or the decimal separator. People in locales that use a different format will be very confused. – rmaddy May 05 '17 at 04:29
  • 1
    And please, never use the words "does not work". It's a meaningless statement. Instead, please clearly and precisely explain what the actual issue is. Do you get errors? Do you get unexpected results? Anything else? Provide complete messages and sample input, expected results, and actual results. [Edit] your question, do not post this info in comments. – rmaddy May 05 '17 at 04:31
  • Your method is creating a `NSNumberFormatter`, configuring it (and like rmaddy said, you really should set the style only, and let the user's locale take care of the rest), but never using it. You presumably want to call `stringFromNumber` of that formatter. Or change your method to return the formatter you just created so the caller can call `stringFromNumber` for that formatter. – Rob May 05 '17 at 04:47
  • @Winter if the number would be 123456 then what should be the output result? – kb920 May 05 '17 at 05:30
  • @Winter what should be your required ouput? – kb920 May 08 '17 at 08:52

1 Answers1

-3

If you want a number like 123,456,789,5 then use shouldChangeCharactersInRange delegate method and write below code inside it.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSString *strTemp = textField.text;
    strTemp = [textField.text stringByReplacingOccurrencesOfString:@"," withString:@""];
    if (strTemp.length%3==0&&![string isEqualToString:@""]&&textField.text.length!=0&&![[textField.text substringWithRange:NSMakeRange(textField.text.length-1, 1)] isEqualToString:@","]) {
        _txtField.text = [_txtField.text stringByAppendingString:@","];
    }
    return YES;
}

enter image description here

kb920
  • 3,039
  • 2
  • 33
  • 44
  • 1
    There are many issues with this code (besides being very hard to read). 1. It makes no effort to properly handle any selection or any cut, copy or paste. 2. It does not honor a user's locale. 3. It makes no attempt to explain what is wrong with the code in the question. – rmaddy May 05 '17 at 04:59
  • This also puts the commas in the wrong place. If the user types `3333` you should see `3,333`. If the user then types another digit, it should become `33,333`. Your code would show `333,3` then `333,33`. – rmaddy May 05 '17 at 05:09
  • When you type 3333 then it will show 333,3 I just tried it! – kb920 May 05 '17 at 05:13
  • If you have 333 in text field and try to add more character suppose 3 then it shows 333,3 just try from your end too. – kb920 May 05 '17 at 05:15
  • 1
    But the desired result is `3,333`, not `333,3`. The whole idea is to format the number like a person would normally write such a number. – rmaddy May 05 '17 at 05:26
  • Also it accepts multiple `,` strings like `,,,,` at any location – RajeshKumar R May 05 '17 at 05:30
  • @RajeshkumarR if we use number keyboard instead of the normal keyboard then there is no "," character. In the normal keyboard, it accepts everything like A-Z,a-z/*-+!@#$%^&*()_={}[]| and more!!!! :) not only "," or multiple like ,,,, – kb920 May 05 '17 at 05:37
  • @kb920 And If i delete 4 after typing 123,4 comma isnt deleted. The result is 123, it should be 123 – RajeshKumar R May 05 '17 at 05:40
  • 1
    @RajeshkumarR Will update answer once we confirm desired output from questionnaire – kb920 May 05 '17 at 05:45
  • 1
    Never rely on a specific keyboard. Even with the number pad a user can pasted any text into the text field. You should look at the answers from the duplicate question. That is what you should be doing. This answer just has far too many issues. – rmaddy May 05 '17 at 05:51