首页 > 代码库 > objective-c 中数据类型之五 数值类(NSNumber及NSNumberFormatter)

objective-c 中数据类型之五 数值类(NSNumber及NSNumberFormatter)

// 聊聊NSNumber, NSNumber可以封装c中基本数据类型 char,int,long,float,BOOL,NSInteger,CGFloat等。
    NSNumber *num = [NSNumber numberWithChar:'a'];
    NSLog(@"%@", num); // 97
    
    num = [NSNumber numberWithInteger:97];
    num = [NSNumber numberWithFloat:97.0];
    num = [NSNumber numberWithBool:YES];
    
    // 不过直接用下面的方式封装会更方便
    num = @'a';
    num = @97;
    num = @97.0;
    num = @YES;
    
    // 既然NSNumber能封装BOOL,NSInteger,CGFloat等基础数据格式,那如何判断NSNumber是什么基础类型呢,接着看
    /*
     + (NSNumber *)numberWithChar:(char)value;
     + (NSNumber *)numberWithUnsignedChar:(unsigned char)value;
     + (NSNumber *)numberWithShort:(short)value;
     + (NSNumber *)numberWithUnsignedShort:(unsigned short)value;
     + (NSNumber *)numberWithInt:(int)value;
     + (NSNumber *)numberWithUnsignedInt:(unsigned int)value;
     + (NSNumber *)numberWithLong:(long)value;
     + (NSNumber *)numberWithUnsignedLong:(unsigned long)value;
     + (NSNumber *)numberWithLongLong:(long long)value;
     + (NSNumber *)numberWithUnsignedLongLong:(unsigned long long)value;
     + (NSNumber *)numberWithFloat:(float)value;
     + (NSNumber *)numberWithDouble:(double)value;
     + (NSNumber *)numberWithBool:(BOOL)value;
     + (NSNumber *)numberWithInteger:(NSInteger)value NS_AVAILABLE(10_5, 2_0);
     + (NSNumber *)numberWithUnsignedInteger:(NSUInteger)value NS_AVAILABLE(10_5, 2_0);
     */
    num = [NSNumber numberWithChar:'a'];
    NSLog(@"%@ %@", NSStringFromClass(num.class), @(num.objCType)); // __NSCFNumber c
    num = [NSNumber numberWithUnsignedChar:'a'];
    NSLog(@"%@ %@", NSStringFromClass(num.class), @(num.objCType)); // __NSCFNumber s
    num = [NSNumber numberWithShort:97];
    NSLog(@"%@ %@", NSStringFromClass(num.class), @(num.objCType)); // __NSCFNumber s
    num = [NSNumber numberWithUnsignedShort:97];
    NSLog(@"%@ %@", NSStringFromClass(num.class), @(num.objCType)); // __NSCFNumber i
    num = [NSNumber numberWithInt:97];
    NSLog(@"%@ %@", NSStringFromClass(num.class), @(num.objCType)); // __NSCFNumber i
    num = [NSNumber numberWithUnsignedInt:97];
    NSLog(@"%@ %@", NSStringFromClass(num.class), @(num.objCType)); // __NSCFNumber q
    num = [NSNumber numberWithLong:97];
    NSLog(@"%@ %@", NSStringFromClass(num.class), @(num.objCType)); // __NSCFNumber i
    num = [NSNumber numberWithUnsignedLong:97];
    NSLog(@"%@ %@", NSStringFromClass(num.class), @(num.objCType)); // __NSCFNumber q
    num = [NSNumber numberWithLongLong:97];
    NSLog(@"%@ %@", NSStringFromClass(num.class), @(num.objCType)); // __NSCFNumber q
    num = [NSNumber numberWithUnsignedLongLong:97];
    NSLog(@"%@ %@", NSStringFromClass(num.class), @(num.objCType)); // __NSCFNumber q
    num = [NSNumber numberWithFloat:1.2];
    NSLog(@"%@ %@", NSStringFromClass(num.class), @(num.objCType)); // __NSCFNumber f
    num = [NSNumber numberWithDouble:1.2];
    NSLog(@"%@ %@", NSStringFromClass(num.class), @(num.objCType)); // __NSCFNumber d
    num = [NSNumber numberWithBool:YES];
    NSLog(@"%@ %@", NSStringFromClass(num.class), @(num.objCType)); // __NSCFBoolean c
    num = [NSNumber numberWithInteger:97];
    NSLog(@"%@ %@", NSStringFromClass(num.class), @(num.objCType)); // __NSCFNumber i
    num = [NSNumber numberWithUnsignedInteger:YES];
    NSLog(@"%@ %@", NSStringFromClass(num.class), @(num.objCType)); // __NSCFNumber i
    
    // 这样就看出来了,判断整型int类,包括short, unsigned short, int, unsigned int, long, unsigned long, long long, unsigned long long,就这样判断
    NSArray *intTypeArray = @[@"s", @"i", @"q"];
    NSString *intType = [NSString stringWithFormat:@"%s", num.objCType];
    if ([NSStringFromClass(num.class) isEqualToString:@"__NSCFNumber"] && [intTypeArray containsObject:intType]) {
        NSLog(@"%@是一个整数", num);
    }
    // 浮点型判断
    NSArray *floatTypeArray = @[@"f", @"d"];
    <pre name="code" class="objc">    NSString *floatType = [NSString stringWithFormat:@"%s", num.objCType];
    if ([NSStringFromClass(num.class) isEqualToString:@"__NSCFNumber"] && [floatTypeArray containsObject:floatType]) {
        NSLog(@"%@是一个浮点型", num);
    }
    // char判断
    NSArray *charTypeArray = @[@"c"];
    NSString *charType = [NSString stringWithFormat:@"%s", num.objCType];
    if ([NSStringFromClass(num.class) isEqualToString:@"__NSCFNumber"] && [charTypeArray containsObject:charType]) {
        NSLog(@"%@是一个字符型", num);
    }
    // BOOL判断
    if ([NSStringFromClass(num.class) isEqualToString:@"__NSCFBoolean"]) {
        NSLog(@"%@是一个字符型", num);
    }
    if ([num isKindOfClass:NSClassFromString(@"__NSCFBoolean")]) {
        NSLog(@"num is BOOL type");
    } else {
        NSLog(@"num is not BOOL type");
    }

    // 下面聊NSNumberFormatter
    // 从学号谈起,一般学号的全格式是 入学年份(前两位)+大学(学院)代码+专业代码+班级+个人班级学号 05+01+02+1+01,给班级学生列成绩单,知道学生班级学号,生成学校学号,如5号,应该生成050102105,我们想到的方法是c中的格式化,例如:
    NSInteger classCode = 5;
    NSString *schoolStuCode = [NSString stringWithFormat:@"0501021%2.2d", classCode];
    NSLog(@"schoolStuCode = %@", schoolStuCode); // 050102105
    
    // 另一种方法是NSNumberFormatter转换,上代码
    // 数据格式转换类
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    
    numberFormatter.numberStyle = kCFNumberFormatterNoStyle;
    numberFormatter.formatWidth = 2;    // 数据宽度为2,不足前面补0
    numberFormatter.paddingCharacter = @"0";
    schoolStuCode = [NSString stringWithFormat:@"0501021%@", [numberFormatter stringFromNumber:@(classCode)]];
    NSLog(@"schoolStuCode = %@", schoolStuCode); // 050102105
    // 这里也输出了050102105
    
    // 之后体验NSNumberFormatter强大功能
    // 字符串分隔,可用于手机号显示
    NSString *formatterStr;
    numberFormatter.numberStyle = kCFNumberFormatterDecimalStyle;
    formatterStr = [numberFormatter stringFromNumber:@102140302];
    NSLog(@"%@", formatterStr); // 102,140,302
    // 从格式字符串转换为数据,其他类似
    NSNumber *number = [numberFormatter numberFromString:formatterStr];
    NSLog(@"number = %@", number);
    // 货币符号
    numberFormatter.numberStyle = NSNumberFormatterCurrencyStyle;
    formatterStr = [numberFormatter stringFromNumber:@1.2];
    NSLog(@"%@", formatterStr); // $1.20
    // 百分数格式
    numberFormatter.numberStyle = kCFNumberFormatterPercentStyle;
    formatterStr = [numberFormatter stringFromNumber:@1.2];
    NSLog(@"%@", formatterStr); // 120%
    // 科学计数格式
    numberFormatter.numberStyle = kCFNumberFormatterScientificStyle;
    formatterStr = [numberFormatter stringFromNumber:@1.2];
    NSLog(@"%@", formatterStr); // 1.2E0
    // 口语格式
    numberFormatter.numberStyle = kCFNumberFormatterSpellOutStyle;
    formatterStr = [numberFormatter stringFromNumber:@1.2];
    NSLog(@"%@", formatterStr); // one point two

objective-c 中数据类型之五 数值类(NSNumber及NSNumberFormatter)