ios - How to validate fractional number -
i trying write function returns boolean value if given string in valid fractional format or not.
e. g. fraction numbers follows 2/3, 1 2/3, 6/5, 80/20, 60 1/4, etc.
-(bool)validatefraction:(nsstring *)string { if(string valid fraction) return yes; else return no; }
you can use regular expressions that:
-(bool)validatefraction:(nsstring *)string{ nsstring *fractionregex = @"\\d+(/\\d+)?"; nspredicate *fractiontest = [nspredicate predicatewithformat:@"self matches %@", fractionregex]; return [fractiontest evaluatewithobject:string]; }
p.s. not also, that function not validate against division 0 , not allow fraction have sign (+ or -) @ front
Comments
Post a Comment