如何限制小数点后的两位数

时间:2016-06-07 07:06:34

标签: ios objective-c uitextfield

我需要允许用户在小数点后只输入两位数。

我刚刚尝试了以下代码,但我没有得到确切的结果。

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    NSArray *sep = [newString componentsSeparatedByString:@"."];
    if([sep count] >= 2)
    {
        NSString *sepStr=[NSString stringWithFormat:@"%@",[sep objectAtIndex:1]];
        return !([sepStr length]>2);
    }

    return YES;
}

我得到以下代码的结果:123.12.1233

但我需要的结果如1)1234.12 2)12.23

注意:我使用数字键盘作为键盘输入视图。

4 个答案:

答案 0 :(得分:2)

我已经修改了你的代码。试试这个

NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
NSArray *sep = [newString componentsSeparatedByString:@"."];
if([sep count] >= 2)
{
    NSString *sepStr=[NSString stringWithFormat:@"%@",[sep objectAtIndex:1]];
    if (!([sepStr length]>2)) {
        if ([sepStr length]==2 && [string isEqualToString:@"."]) {
            return NO;
        }
        return YES;
    }
    else{
        return NO;
    }
}

答案 1 :(得分:1)

与大多数语言一样,格式为

%.2f

例如

NSString* NumberStr = [NSString stringWithFormat:@"%.02f", yourFloatValue];

%.02f告诉格式化程序您将格式化浮点数(%f),并且应该舍入到两个位置。

示例:

%f = 5.000000
%.f = 5
%.02f = 5.00

答案 2 :(得分:1)

以下方法不允许more than two digits after dot,它不允许dot at first position or as first character,它不会允许任何字符而不是number or dot

 -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{




NSCharacterSet *nonNumberSet = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789."] invertedSet];

// allow backspace
if (range.length == 0 && [string length] == 0) {
    return YES;
}

if ([string isEqualToString:@"."] ){

    if (range.location == 0) {
        return NO;
    }
    if(dotLocation == 0){

        dotLocation = range.location;
        return YES;
    }
    else{
        return NO;
    }

}
if (range.location == dotLocation && string.length==0) {

    dotLocation = 0;

}

if (dotLocation > 0 && range.location > dotLocation+2) {
    return NO;
}


NSString *newValue = [[textField text] stringByReplacingCharactersInRange:range withString:string];
newValue = [[newValue componentsSeparatedByCharactersInSet:nonNumberSet] componentsJoinedByString:@""];
textField.text = newValue;

return NO;
}

在此方法中,dotLocationint类型的实例变量。只需声明实例Variale int dotLocation

  @interface ViewController () <UITextFieldDelegate>
 {

 int dotLocation;
 }

就是这样,这种方法可以正常工作。

第二件事是确保你已经在viewDidload中将textField的委托设置为self,

 yourTextField.delegate = self;

希望这会有所帮助:)

答案 3 :(得分:-1)

尝试使用Regex:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    NSString *expression = @"^[0-9]*((\\.|,)[0-9]{0,2})?$";
    NSError *error = nil;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:expression options:NSRegularExpressionCaseInsensitive error:&error];
    NSUInteger numberOfMatches = [regex numberOfMatchesInString:newString options:0 range:NSMakeRange(0, [newString length])];
    return numberOfMatches != 0;
}