-1

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

Eric
  • 3,811
  • 5
  • 15
  • 18
  • Please try to improve your question so that other users can clearly understand your problem and be able to help you. It would really help if you'd show code for what you've tried already. Have a look at [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) – Gad Jun 10 '15 at 06:30
  • Possible Duplicate of http://stackoverflow.com/questions/30742666/format-currency-with-uitextfield – MPG Jun 10 '15 at 06:51

1 Answers1

0

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.

Jatin Patel - JP
  • 3,725
  • 2
  • 21
  • 43