开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜