如何在键入时将文本字段格式设置为十进制? [英] How to set the textfield format as decimal while typing?

查看:109
本文介绍了如何在键入时将文本字段格式设置为十进制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.


假设用户输入 5078 在输入时会显示 50.78 格式。

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


推荐答案

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

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.

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;

这篇关于如何在键入时将文本字段格式设置为十进制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆