// // NSNumberAdditions.m // JSRun Import Framework // // Created by Jonathan Saggau on 8/31/06. // Copyright 2006 Jonathan Saggau. All rights reserved. // #import "NSNumberAdditions.h" @implementation NSNumber (NSNumberAdditions) + (NSNumber *)numberWithString:(NSString *)aString; { NSScanner *scanner; int scanLocation = 0; NSMutableString *string = [NSMutableString stringWithString:aString]; [string replaceOccurrencesOfString:@" " withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [string length])]; //remove spaces scanner = [NSScanner scannerWithString:string]; if ([string hasPrefix:@"$"]) scanLocation = 1; // just in case we're given a dollar value int intResult; [scanner setScanLocation:scanLocation]; if ([scanner scanInt:&intResult] && ([scanner scanLocation] == [string length] )) { return [NSNumber numberWithInt:intResult]; } float floatResult; [scanner setScanLocation:scanLocation]; if ([scanner scanFloat:&floatResult] && ([scanner scanLocation] == [string length] )) { return [NSNumber numberWithFloat:floatResult]; } long long longLongResult; [scanner setScanLocation:scanLocation]; if ([scanner scanLongLong:&longLongResult] && ([scanner scanLocation] == [string length] )) { return [NSNumber numberWithLongLong:floatResult]; } NSLog(@"Couldn't convert %@ to nsnumber", string); return [NSNumber numberWithInt:0]; } @end