I've found the code below, which I'm trying to make work in the UITableView shouldChangeCharactersInRange event. Without the currency symbol it works fine.
When I try to add the currency symbol, it just allows me to enter one number. What it should do is this, say I enter 7536, it should appear in the following steps £0.07, £0.75, £7.53, £75.36 but instead it appears as £0.07, £0.05, £0.03, £0.06
Heres the code.
- (BOOL)textField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string
{ BOOL res = TRUE;
double currentValue = [textField.text doubleValue];
double cents = round(currentValue * 100.0f);
if ([string length]) {
for (size_t i = 0; i < [string length]; i++) {
unichar c = [string characterAtIndex:i];
if (isnumber(c)) {
cents *= 10;
cents += c - '0';
}
}
} else {
// back Space
cents = floor(cents / 10);
}
// This line next works without the currency symbol
//textField.text = [NSString stringWithFormat:@"%.2f", cents / 100.0f];
NSMutableString *aString = [NSMutableString stringWithFormat:@"%@", strCurrencySymbol];
[aString appendFormat:@"%.2f", cents / 100.0f];
textField.text = aString;
res = NO;
return res;
}