在我的应用程序中,我有一个文本字段,当我单击该文本字段时,数字小键盘将打开。现在我的问题是如何在键入时将该值转换为十进制格式,因为我必须仅插入小数值和数字小键盘没有给出点 (.)。因此当用户在文本字段中键入时,它会自动将该值转换为十进制格式。
Suppose if user type 5078 it will show 50.78 format while typing.
最佳答案
您可以简单地将数字乘以“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;
关于iphone - 打字时如何将文本字段格式设置为十进制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7441315/