1

I want to add a ',' in '11000' like this '11,000' and decimal ('.') in 465 like 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 this doing is it actually putting the comma like this 1,1000 for 11000. And i am not able to do anything close for decimal. Please help!!

Lyndsey Scott
  • 37,080
  • 10
  • 92
  • 128
Ashutosh
  • 5,614
  • 13
  • 52
  • 84
  • Calling `[unformattedValue intValue]` will always give you an integer value, you're stripping anything after the decimal from your input so you will never see it in your output. – Jonah Sep 11 '12 at 18:21

2 Answers2

2

NSNumberFormatter can handle this conversion from string to number and back for you. No need to strip characters yourself with stringByReplacingOccurrencesOfString or use less lenient string to numeric conversion methods like intValue (and at the very least don't use intValue if you want to be able to get a decimal).

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];

NSNumber *amount = [formatter numberFromString:textField.text];

textField.text = [formatter stringFromNumber:amount];

Depending on the input you need to tolerate you might still want to do some other cleanup of the input string if NSNumberFormatter's lenient setting is not enough. You could also use multiple number formatters if you wanted to parse an input string in one format and then output it in another.

Jonah
  • 17,918
  • 1
  • 43
  • 70
0

Use the following code to manually add commas at the right locations. You can get the logic from this code and tweak it to suit your requirement.

-(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;
}
Varoon
  • 172
  • 1
  • 5