Karthikeya
Karthikeya

Reputation: 83

How can i display the number in such a format?

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;
}

Upvotes: 0

Views: 370

Answers (2)

Caleb
Caleb

Reputation: 124997

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.

Upvotes: 0

sch
sch

Reputation: 27506

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);

Upvotes: 1

Related Questions