List of all Iphone and Blackberry Development codes in a one click Iphone,objective C,xcode,blackberry Development,iOS Development

Friday, July 5, 2019

iOS Decimal in Textfield

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSString *str =@"0.00";
    if([string isEqual:@""]){
        if(textField.text.length==0){
            textField.text= str;
            return NO;
        }else{
            NSArray *items = [[NSString stringWithFormat:@"%f",[textField.text doubleValue]*0.1] componentsSeparatedByString:@"."];

            NSMutableArray *array = [NSMutableArray array];
            NSString *str = items[1];
            for (int i = 0; i < [str length]; i++) {
                NSString *ch = [str substringWithRange:NSMakeRange(i, 1)];
                [array addObject:ch];
            }

            str=[NSString stringWithFormat:@"%@.%@%@",items[0],array[0],array[1]];
            textField.text= str;
            return NO;
        }
    }else{

        if(textField.text.length==0){
             float f = [[NSString stringWithFormat:@"%@%@",textField.text,string] floatValue];
            str=[NSString stringWithFormat:@"%.2f",f*0.01];
            textField.text= str;
        }else{
            //str=[NSString stringWithFormat:@"%.2f",f*10];
            NSString *fullText=[NSString stringWithFormat:@"%@%@",textField.text,string];

            NSArray *items = [fullText componentsSeparatedByString:@"."];

            NSMutableArray *array = [NSMutableArray array];
            NSString *str = items[1];
            for (int i = 0; i < [str length]; i++) {
                NSString *ch = [str substringWithRange:NSMakeRange(i, 1)];
                [array addObject:ch];
            }

            NSString *firstValue=@"";
            if([items[0] isEqualToString:@"0"]){
                firstValue=array[0];
            }else{
                firstValue=items[0];
                firstValue=[firstValue stringByAppendingString:array[0]];
            }
            NSString *strVal=[NSString stringWithFormat:@"%@.%@%@",firstValue,array[1],array[2]];
            str=strVal;
            textField.text= str;
        }

        return NO;
    }
    textField.text= str;
   // textField.text= [NSString stringWithFormat:@"%f",f*0.01];
    return NO;



}