Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check if the NSString is integer or double

how can I check if the NSString *val I have has a Integer or float ?? The raw approach is to look for "." character..but is there a more graceful way to do this?

like image 784
A B Vijay Kumar Avatar asked Mar 22 '26 03:03

A B Vijay Kumar


1 Answers

First, try [NSScanner scanInt:]&& [NSScanner isAtEnd]. If it returns YES, then you have an int. scanInt will scan forward as long as it can interpret the stream as an int. If isAtEnd is YES, then the entire string could be interpreted as an int (so you have an int).

Otherwise, try [NSScanner scanDouble:]. If it returns YES, then you have a double.

If both return NO, then you don't have either.

like image 85
Alex Churchill Avatar answered Mar 23 '26 19:03

Alex Churchill