Ios – Trying to format a UITextField to behave like a currency calculator for only numeric input in iOS

currencyiosnsnumberformatteruitextfield

I have a UITextField in my application that is supposed to receive only numeric input from the user. This numeric input is supposed to represent currency (i.e. have only two decimal places), have the default value of 0.00 when nothing has been entered. I also would like to have a "$" sign that is on the far left, with the number right aligned, such that the number moves from right to left like such:

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.

8. and so on…

I have the UITextField right aligned so that it already moves the numbers over to the left from the right. However, I need to place the "$" inside the UITextField, and I would like the default 0.00 value to be modified to the number entered by the user one digit at a time. The user should NOT have to enter the decimal point, as it should already be in the textfield holding its position. I then would like the number to have a "," to be automatically entered to separate every three digits. I would like the application to do this AS THE USER IS ENTERING THE NUMBERS, and not formatted afterwards when everything is entered. How would I do this? I have seen a lot of people say that I am to use NSNumberFormatter, but I want to know how do I use it in conjunction with the UITextField such that it formats the numeric text on the fly, as described above?

I have found the following question posted on StackOverflow some time ago:

What is the best way to enter numeric values with decimal points?

but I want to know how to actually incorporate it as a solution to my problem.

Best Answer

Here's a nice way to do it. Remember to set your UITextField to self in the viewDidLoad method and your header file must conform to the UITextFieldDelegate protocol

-(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;
}
Related Topic