NSNumber *myNumber,*floatNumber,*intNumber;
NSInteger myInt;
//integer value
intNumber = [NSNumber numberWithInteger:100];
myInt = [intNumber integerValue];
NSLog(@"%li",(long)myInt);
//long value
myNumber = [NSNumber numberWithLong:0xabcdef];
NSLog(@"%#lx",[myNumber longValue]);
//char value
myNumber = [NSNumber numberWithChar:'X'];
NSLog(@"%c",[myNumber charValue]);
//float value
floatNumber = [NSNumber numberWithFloat:100.00];
NSLog(@"%g",[floatNumber floatValue]);
//double
myNumber = [NSNumber numberWithDouble:12345e+15];
NSLog(@"%lg",[myNumber doubleValue]);
//wrong access
//NSLog(@"%i", [myNumber integerValue]);
//test two numbers for equality
if([intNumber isEqualToNumber:floatNumber] == YES)
NSLog(@"is equal");
else
NSLog(@"is not equal");
// > < ==
if([intNumber compare:myNumber] == NSOrderedAscending)
NSLog(@"first is less than the second");
iOS基础一 NSNumber
最新推荐文章于 2019-07-23 11:43:00 发布