Inserting a comma for integers when there are three digits added everytime to the textfield in iOS

StackOverflow https://stackoverflow.com/questions/15827378

Pregunta

I need to add comma separating numbers into three digits without white spaces and if total digits are under three, then no comma added.

For example:

2984 => 2,984
297312984 => 297,312,984
298 => 298

How do I solve this?

Tried this:

if([textfield.text length] > 3)
{
 NSMutableString *stringtext = [NSMutableString stringWithString:textfield.text];
 [stringtext insertString:@"," atIndex:0];
}

Abt after starting, started to think if there is a better solution? This is because there will be a lot of if-else statements.

Welcome to any suggestion.

¿Fue útil?

Solución

This is what I've come up with: just set the delegate of the text field, and implement the following method accordingly:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField.text.length % 4 == 3) {
        if (string.length != 0) {
            textField.text = [NSString stringWithFormat:@"%@,%@", textField.text, string];
            return NO;
        }
    }

    return YES;
}

Otros consejos

Use NSNumberFormatter:

NSNumberFormatter *numberFormatter = [NSNumberFormatter new];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSString *commaString = [numberFormatter stringFromNumber:[NSNumber numberWithInteger:yourIntegerValue]];
NSLog(@"---> %@",commaString);

Or by string manipulation <This is not the best way>:

NSMutableString *mutString=[NSMutableString stringWithString:@"123456789"];

NSMutableArray *array=[NSMutableArray new];
for (NSInteger i=1; i<=mutString.length/3; i++) {
    NSInteger index=[mutString length]-i*3;
    [array insertObject:[mutString substringWithRange:NSMakeRange(index, 3)] atIndex:0];
}
if (mutString.length!=mutString.length/3*3) {
    [array insertObject:[mutString substringWithRange:NSMakeRange(0, mutString.length%3)] atIndex:0];
}
NSString *commaString=[array componentsJoinedByString:@","];
NSLog(@"--> %@",commaString);

Use NSNumberFormatter,

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

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

[formatter release];

Please try this following function

    //------------------------------------------------------------------------
    // Method : ChangeNumberIntoCommaSeparatedStringDec
    // Method to convert number into comma separated string
    //------------------------------------------------------------------------
    -(NSString *)ChangeNumberIntoCommaSeparatedStringDec:(double)num
    {

            NSNumber *number = [NSNumber numberWithDouble:num];
            NSNumberFormatter *frmtr = [[NSNumberFormatter alloc] init];
           //here locale as applied to do not change the formatter values after changing the device locale
            [frmtr setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"en_US"]];
            [frmtr setGroupingSize:3];
            [frmtr setGroupingSeparator:@","];
            [frmtr setUsesGroupingSeparator:YES];
            [frmtr setMaximumFractionDigits:2];
            [frmtr setMinimumFractionDigits:2];
            NSString *str=[frmtr stringFromNumber:number];
            [frmtr release];
            return(str);        
    }
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top