Uso [NSNumberFormatter numberFromString: s]
. Devuelve nil si la cadena especificada no es numérica. Puede configurar NSNumberFormatter para definir "numérico" para su escenario particular.
#import <Foundation/Foundation.h>
int
main(int argc, char* argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSLocale *l_en = [[NSLocale alloc] initWithLocaleIdentifier: @"en_US"];
NSLocale *l_de = [[NSLocale alloc] initWithLocaleIdentifier: @"de_DE"];
NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
[f setLocale: l_en];
NSLog(@"returned: %@", [f numberFromString: @"1.234"]);
[f setAllowsFloats: NO];
NSLog(@"returned: %@", [f numberFromString: @"1.234"]);
[f setAllowsFloats: YES];
NSLog(@"returned: %@", [f numberFromString: @"1,234"]);
[f setLocale: l_de];
NSLog(@"returned: %@", [f numberFromString: @"1,234"]);
[l_en release];
[l_de release];
[f release];
[pool release];
}
¿Qué cuenta como número? Enteros? ¿Punto flotante? ¿Notación cientifica? Hexadecimal con "0x" adelantado Binario con "0b" adelantado – outis