Skip to content

Instantly share code, notes, and snippets.

@galileoguzman
Last active March 29, 2016 06:50
Show Gist options
  • Save galileoguzman/de2cf67cdecafac00aad to your computer and use it in GitHub Desktop.
Save galileoguzman/de2cf67cdecafac00aad to your computer and use it in GitHub Desktop.
Trying to format a UITextField to behave like a currency calculator for only numeric input in iOS

e.g. when entering the number "1234.56".

  1. $ 0.00.

  2. $ 0.01.

  3. $ 0.12.

  4. $ 1.23.

  5. $ 12.34.

  6. $ 123.45.

  7. $ 1,234.56

         -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
         
               NSString *cleanCentString = [[textField.text componentsSeparatedByCharactersInSet: [[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:@""];
                 NSInteger centValue = [cleanCentString intValue];
                 NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
                 NSNumber *myNumber = [f numberFromString:cleanCentString];
                 NSNumber *result;
                 
                 if([textField.text length] < 16){
                     if (string.length > 0)
                     {
                         centValue = centValue * 10 + [string intValue];
                         double intermediate = [myNumber doubleValue] * 10 +  [[f numberFromString:string] doubleValue];
                        result = [[NSNumber alloc] initWithDouble:intermediate];
                     }
                     else
                     {
                         centValue = centValue / 10;
                         double intermediate = [myNumber doubleValue]/10;
                         result = [[NSNumber alloc] initWithDouble:intermediate];
                     }
                     
                     myNumber = result;
                      NSLog(@"%ld ++++ %@", (long)centValue, myNumber);
                         NSNumber *formatedValue;
                         formatedValue = [[NSNumber alloc] initWithDouble:[myNumber doubleValue]/ 100.0f];
                         NSNumberFormatter *_currencyFormatter = [[NSNumberFormatter alloc] init];
                         [_currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
                         textField.text = [_currencyFormatter stringFromNumber:formatedValue];
                         return NO;
                 }else{
                 
                     NSNumberFormatter *_currencyFormatter = [[NSNumberFormatter alloc] init];
                     [_currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
                     textField.text = [_currencyFormatter stringFromNumber:00];
                     
                     UIAlertView *alert = [[UIAlertView alloc]initWithTitle: @"Deposit Amount Limit"
                                                                    message: @"You've exceeded the deposit amount limit. Kindly re-input amount"
                                                                   delegate: self
                                                          cancelButtonTitle:@"Cancel"
                                                          otherButtonTitles:@"OK",nil];
                                                          
                     [alert show];
                     return NO;
                 }
                 return YES;
         }
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment