xcode 打字时如何将文本字段格式设置为十进制?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7441315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:56:41  来源:igfitidea点击:

How to set the textfield format as decimal while typing?

iphoneobjective-cxcode

提问by Developer

In my app i have one text field and when i click on that text field numeric keypad will open.now my question is how to convert that value in decimal format while typing as i have to insert only decimal values and in numeric keypad dot(.)is not given.so when user type in textfield it automatically convert that value into decimal format.

在我的应用程序中,我有一个文本字段,当我单击该文本字段时,数字小键盘将打开。现在我的问题是如何在键入时将该值转换为十进制格式,因为我必须仅插入十进制值和数字小键盘点 (. ) 未给出。因此,当用户在文本字段中键入时,它会自动将该值转换为十进制格式。

Suppose if user type 5078it will show 50.78format while typing.

假设如果用户键入5078,它将在键入时显示50.78格式。

回答by EmptyStack

You can simply multiply the number by "0.01" (for two decimal places) and use the string format "%.2lf". Write the following code in textField:shouldChangeCharactersInRange:withString:method.

您可以简单地将数字乘以“ 0.01”(两位小数)并使用字符串格式“ %.2lf”。在textField:shouldChangeCharactersInRange:withString:方法中编写以下代码。

NSString *text = [textField.text stringByReplacingCharactersInRange:range withString:string];
text = [text stringByReplacingOccurrencesOfString:@"." withString:@""];
double number = [text intValue] * 0.01;
textField.text = [NSString stringWithFormat:@"%.2lf", number];
return NO;

回答by userar

try this.

尝试这个。

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

    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);
    }

    textField.text = [NSString stringWithFormat:@"%.2f", cents / 100.0f];
     if(cents==0)
    {
        textField.text=@"";
        return YES;
    }
    return NO;
    }

回答by Eduardo Fabricio

Thanks userar, it works fine for me. Im my case i need to format the decimal like a currency localized format when finish the editing.

感谢用户,它对我来说很好用。我的情况是,我需要在完成编辑后将小数格式化为货币本地化格式。

- (BOOL) textFieldShouldEndEditing:(UITextField *)textField {

      NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
      formatter.numberStyle = NSNumberFormatterCurrencyStyle;

      // im my case i need specify the currency code, 
      // but could have got it from the system.
      formatter.currencyCode = @"BRL";

      NSDecimalNumber *decimalNumber = 
            [NSDecimalNumber decimalNumberWithString:textField.text];

     // keeping the decimal value for submit to server.
     self.decimalValue = decimalNumber;

     // formatting to currency string.
     NSString * currencyString = [formatter stringFromNumber:decimalNumber];
     textField.text = currencyString;

}

}