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

标签 iphone objective-c xcode

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

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/

相关文章:

ios - NSUserDefault BoolKey 未保存在关闭应用程序后重置

ios - 如何以编程方式更改 XCode 项目的设备目标?

iphone - 调整 iPhone 5 中的表格 View 大小

iphone - 我应该在 +initialize 中调用 super 吗?

iphone - iOS:检测WIFI是否通过认证?

iphone - IOS 应用程序 : Sending data to web server constantly

iPhone 6.1 模拟器

android - play-games-plugin-for-unity - 显示特定排行榜

swift - 如果单击禁用的 UIButton 会发生什么

ios - 在 Sprite Kit 中暂停某些 Action