我最终使用了我在 StackOverflow 上找到的一点点
shouldChangeCharactersInRange
双倍电流值;
NSString *str;
if(![textField.text length]){
str = @"$ 0.00";
}else{
str = textField.text;
}
currentValue = [[str substringFromIndex:1] 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);
}
textField.text = [NSString stringWithFormat:@"%.2f", cents / 100.0f];
//Add this line
[textField setText:[NSString stringWithFormat:@"$%@",[textField text]]];
return NO;