限制输入,输入金额 和保留小数点后两位
//判断是否是浮点类型
+ (BOOL)isPureFloat:(NSString*)string {
NSScanner* scan = [NSScanner scannerWithString:string];
float val;
return [scan scanFloat:&val] && [scan isAtEnd];
}
//在textFiled中限制输入位数
if ([string isEqualToString:@""]) {
return YES;
}
NSString *text = [textField.text stringByReplacingCharactersInRange:range withString:string];
if ([MyTool isPureFloat:text]) {
if ([text doubleValue] - [_totalPrice doubleValue] > DBL_EPSILON) {
return NO;
}else {
//小数点后两位
NSUInteger location = [text rangeOfString:@"."].location;
if (location != NSNotFound && text.length > location + 3) {
return NO;
}else {
return YES;
}
}
}else {
return NO;
}