I’m using following code to converted entered number in text field to be formatted to show USD Currency. So it shows the $ sign and for every 3 digits (from right) a , is added. I’m setting the formatted text in text field.
- (void)textFieldDidEndEditing:(UITextField *)textField
{
NSString *pureNumbers = [[textField.text componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:@""];
//Convert entered text to NSNumber
NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber * myNumber = [f numberFromString:pureNumbers];
NSString *currencyString = [currencyFormatter internationalCurrencySymbol];
NSString *format = [currencyFormatter positiveFormat];
format = [format stringByReplacingOccurrencesOfString:@"*" withString:currencyString];
// ¤ is a placeholder for the currency symbol
[currencyFormatter setPositiveFormat:format];
NSString *currencyString1 = [currencyFormatter stringFromNumber:myNumber];
NSLog(@"Text is %@",currencyString1);
textField.text = currencyString1;
}
Now how can I get the back the number entered removing the number formatter?
Example:
I entered 5000 in text field. Its formatted to $5,000 in the text field.
I want to get back “5000” from the text field which is the original value entered by user.
I’m guessing you copied and pasted most of your formatting function from somewhere rather than writing it yourself, because it already contains the code to do this. In the first line you say:
This takes a string and strips out any non-numeric characters. So this already does exactly what you want. E.g. if you say
The pureNumbers string now equals @”5000″. To convert that to numeric value that you can actually do math with, you can say: