0

I am displaying a number in textfield. Which displays the number as "1234" but i want to display it as in format of "1,234" if i enter another large number which displays as "12345" but i want to display it as "12,345" if i enter 123456 which has to display as "123,456" . How do I format this number in desired format?

-(void)clickDigit:(id)sender
{
    NSString * str = (NSString *)[sender currentTitle];
    NSLog(@"%@",currentVal);


    if([str isEqualToString:@"."]&& !([currentVal rangeOfString:@"."].location == NSNotFound) ) 
    {
        return;
    }
    if ([display.text isEqualToString:@"0"])
    {

        currentVal = str;
        [display setText:currentVal];
    }

    else if([currentVal isEqualToString:@"0"])
    {
        currentVal=str;
        [display setText:currentVal];

    }
    else
    {
        if ([display.text length] <= MAXLENGTH) 
        {
            currentVal = [currentVal stringByAppendingString:str];
            NSLog(@"%@",currentVal);
            [display setText:currentVal];
        }
        currentVal=display.text;
    }
}

This is the code i am using to display the number in textfield.


EDIT: I Changed my code into the following but still don't get the number correctly formatted:

if ([display.text length] <= MAXLENGTH) {
    currentVal = [currentVal stringByAppendingString:str];
    NSNumberFormatter * myNumFormatter = [[NSNumberFormatter alloc] init];
    [myNumFormatter setNumberStyle:NSNumberFormatterDecimalStyle]; 
    NSNumber *tempNum = [myNumFormatter numberFromString:currentVal];
    NSLog(@"My number is %@",tempNum);
    [display setText:[tempNum stringValue]];
    currentVal=display.text;
}
sch
  • 27,436
  • 3
  • 68
  • 83
Karthikeya
  • 83
  • 1
  • 2
  • 8
  • Check out the question at http://stackoverflow.com/questions/5406366/formatting-a-number-to-show-commas-and-or-dollar-sign - the accepted answer http://stackoverflow.com/a/5407103/928098 looks like it'll solve your problem – Kristian Glass Mar 17 '12 at 12:42
  • I think OP doesn't need a dollar sign in the displayed string as in the answer you linked to. – sch Mar 17 '12 at 13:03

2 Answers2

1

You can do it like this:

int myInt = 12345;
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.numberStyle = NSNumberFormatterDecimalStyle;
NSNumber *number = [NSNumber numberWithInt:myInt];
NSLog(@"%@", [formatter stringFromNumber:number]); // 12,345

Edit

You didn't implement this correctly, the key is to obtain the string representation of the number using [formatter stringFromNumber:number], but you didn't do that. So change your code into:

currentVal = [currentVal stringByAppendingString:str];
NSNumberFormatter * myNumFormatter = [[NSNumberFormatter alloc] init];
[myNumFormatter setNumberStyle:NSNumberFormatterDecimalStyle]; 
NSNumber *tempNum = [myNumFormatter numberFromString:currentVal];
NSLog(@"My number is %@",tempNum);
[display setText:[myNumFormatter stringFromNumber:tempNum]]; // Change this line
currentVal=display.text;
NSLog(@"My formatted number is %@", currentVal);
sch
  • 27,436
  • 3
  • 68
  • 83
  • Its not giving the desired result .. still it displays as 12345 not getting comma as a separator. I think we have to add some string separator to it – Karthikeya Mar 17 '12 at 13:26
  • @Karthikeya - This is strange, can you post your updated code? – sch Mar 17 '12 at 13:39
  • yeah it works fine for first time like if i enter 1234 giving result as 1,234 but second time current value is becoming null – Karthikeya Mar 17 '12 at 14:01
  • @sch Sorry about that -- you're right that NSNumberFormatterDecimalStyle will do the right thing. I expect it's better in general to do that, since it probably does use the correct settings for the user's region. – Caleb Mar 17 '12 at 14:11
  • @Karthikeya - If it gives you `null` I guess the problem is that `currentVal` is not correctly formatted. After all, you add some digits to the original number so `1,234` will become `1,2345` which is not parsed correctly. – sch Mar 17 '12 at 14:15
  • @sch -Thank you , problem solved, I had to remove last but one line. current value will be formatted as we want – Karthikeya Mar 19 '12 at 06:50
0

First, read through the list of methods on the NSNumberFormatter reference page. After doing that, you'll probably realize that you need to use the -setHasThousandSeparators: method to turn on the thousand separators feature. You can also use the -setThousandSeparator: method to set a custom separator, though you probably won't need to do that.

Caleb
  • 124,013
  • 19
  • 183
  • 272