IOS TextField 根据输入,分割整数与小数部分

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString *valueStr = nil;
    if (string.length > 0) 
    {
        if (textField.text.length >6) {
            return NO;
        }
        else {
            NSArray *tmp = [string componentsSeparatedByString:@"."];//分割字符串
            if (tmp.count > 1) {
                return NO;
            }
            NSString *newString = nil;
            newString = [string stringByReplacingOccurrencesOfRegex:@"[^0-9.]" withString:@""];
            if (newString == @"") {
                return NO;
            }
            valueStr = [NSString stringWithFormat:@"%.0f",[textField.text doubleValue]*10];
        }
        textField.text = [valueStr stringByAppendingFormat:@"."];
    }
    else {
        valueStr = [NSString stringWithFormat:@"%.2f",[textField.text doubleValue]/10];
        textField.text = valueStr;
    }
    return YES;
}

  IOS TextField 根据输入,分割整数与小数部分_第1张图片

你可能感兴趣的:(ios,String)