Having trouble with currency

44 views Asked by At

I have a UITextField that the user will enter an amount of money. I want to set it so it will show the users current currency.

Here is a link to my project. It gives me weird results: jumpShare

1

There are 1 answers

3
Jatin Patel - JP On

Actually you have tried to set decimal fraction at beginning so its make some amount like $1.00. so you can not add more digit in UITextField. so removed that things. add some more modification like as bellowed.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{

    if (![string isEqualToString:@""]) {

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{



            NSNumberFormatter *currencyFormatter = [[NSNumberFormatter alloc] init];
            [currencyFormatter setLocale:[NSLocale currentLocale]];
            [currencyFormatter setMaximumFractionDigits:0];
            [currencyFormatter setAlwaysShowsDecimalSeparator:NO];
            [currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];

            textField.text = [textField.text stringByReplacingOccurrencesOfString:currencyFormatter.currencySymbol withString:@""];

            NSCharacterSet *nonNumbersSet = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789"] invertedSet];

            textField.text = [textField.text stringByTrimmingCharactersInSet:nonNumbersSet];


            NSNumber *someAmount = [NSNumber numberWithUnsignedInteger:[[textField.text stringByReplacingOccurrencesOfString:@"," withString:@""] longLongValue]];
            NSString *t = [currencyFormatter stringFromNumber:someAmount];
            self.textField.text = t;



        });
    }

    return YES;
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{

    return YES;
}

Replace this two methods, may this solved your issue.