Format Interger with two decimal format in UITextfield
I am presenting the user with a field that displays the number keyboard and only want them to be able to enter numbers with decimal it should be only one. After decimal it is should be atleast 2 digits will be there. There should be atleast one digit before the decimal.Then the input can`t be 0 or 0.00. Suppose if user enter the number 123456 it is accepted then 123.56 it also accepted if user enter 123 and then it it take 000123 and how can i tak开发者_Python百科e maximum length of the textfield is 6 and minimum is 1 please help me this one...
Thanks in advance
check with this condition and add prefix
NSLog(@"%d",[textfield.text length]);
if(length<0)
{
add prefix for remaining;
}
I have created a logic for you.You can use this:;-
NSString *string=yourtextField.text;//it will contain your text field text
int stringLength=[string length];
if (stringLength>6) {
NSLog(@"error");
}
else if(stringLength>1)
{
int n=6-stringLength;
for (int i=0; i<n; i++) {
NSString *newString=[NSString stringWithFormat:@"0%@",string];
string=newString;
}
NSLog(@"%@",string);
}
else {
NSLog(@"empty string");
}
Hope this will help you.
精彩评论