3

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.

user7388
  • 1,741
  • 2
  • 19
  • 25
lakshmen
  • 28,346
  • 66
  • 178
  • 276
  • 2
    If you want to honor locale, you should perhaps rephrase your question and ask for on-the-fly update of input in textfield that would insert thousands group separator for you. And for that NSNumberFormatter is good choice. – svena Apr 05 '13 at 07:15

4 Answers4

3

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;
}
  • @AnoopVaidya Thanks, that's what I thought. –  Apr 05 '13 at 06:59
  • This the differnce between you and me, If I assume and solve, i get at least 3 downvotes. hahaa. But for you always +5. :D – Anoop Vaidya Apr 05 '13 at 07:00
  • @AnoopVaidya (you should have seen me being massively downvoted at some C++ questions...) –  Apr 05 '13 at 07:01
  • @lakesh Thanks :) You'll get here quick :) –  Apr 05 '13 at 07:24
  • @Viral: My all time dowvotes shows 26, and i think 20 will be for questions. – Anoop Vaidya Apr 05 '13 at 07:39
  • See [here](http://stackoverflow.com/questions/15811007/how-can-i-get-date-of-a-coming-day-objective-c/15812383#15812383), if that would be my answer, surely -5. Ohhh in this answer I started getting dnvotes....congrats me :) – Anoop Vaidya Apr 05 '13 at 07:41
1

Use NSNumberFormatter,

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

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

[formatter release];
Hasintha Janka
  • 1,608
  • 1
  • 14
  • 27
1

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);
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
0

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);        
    }
svrushal
  • 1,612
  • 14
  • 25