Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective c: Check if integer/int/number

In objective c, how can i check if a string/NSNumber is an integer or int

like image 911
Daniel Avatar asked Aug 13 '10 03:08

Daniel


People also ask

How to check if string is integer in C?

Using built-in method isdigit(), each character of string is checked. If the string character is a number, it will print that string contains int. If string contains character or alphabet, it will print that string does not contain int.

How to check if it is integer in js?

The Number. isInteger() method determines whether the passed value is an integer.

How to check whether the given input is integer or not in python?

Use the str. isdigit() method to check if a user input is an integer. The isdigit() method will return True for all positive integer values and False for all non-numbers, floating-point numbers or negative numbers.


1 Answers

If you're trying to determine whether or not an NSString has a numeric value or not, try using NSNumberFormatter.

-(BOOL) stringIsNumeric:(NSString *) str {
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    NSNumber *number = [formatter numberFromString:str];
    [formatter release];
    return !!number; // If the string is not numeric, number will be nil
}
like image 103
Anshu Chimala Avatar answered Nov 05 '22 02:11

Anshu Chimala