iOS-NSNumberFormatter

iOS中NSDateFormatter用的范围一般来说比较广泛,不过相对于处理数字而言,很少用到NSNumberFormatter,NSNumberFormatter中有很多枚举类型,会为实际开发节省时间.

NSNumberFormatterStyle

简单看下NSNumberFormatterStyle枚举:

typedef NS_ENUM(NSUInteger, NSNumberFormatterStyle) {
    NSNumberFormatterNoStyle = kCFNumberFormatterNoStyle,
    NSNumberFormatterDecimalStyle = kCFNumberFormatterDecimalStyle,
    NSNumberFormatterCurrencyStyle = kCFNumberFormatterCurrencyStyle,
    NSNumberFormatterPercentStyle = kCFNumberFormatterPercentStyle,
    NSNumberFormatterScientificStyle = kCFNumberFormatterScientificStyle,
    NSNumberFormatterSpellOutStyle = kCFNumberFormatterSpellOutStyle,
    NSNumberFormatterOrdinalStyle NS_ENUM_AVAILABLE(10_11, 9_0) = kCFNumberFormatterOrdinalStyle,
    NSNumberFormatterCurrencyISOCodeStyle NS_ENUM_AVAILABLE(10_11, 9_0) = kCFNumberFormatterCurrencyISOCodeStyle,
    NSNumberFormatterCurrencyPluralStyle NS_ENUM_AVAILABLE(10_11, 9_0) = kCFNumberFormatterCurrencyPluralStyle,
    NSNumberFormatterCurrencyAccountingStyle NS_ENUM_AVAILABLE(10_11, 9_0) = kCFNumberFormatterCurrencyAccountingStyle,
};

代码测试:

    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    formatter.numberStyle = NSNumberFormatterDecimalStyle;
    NSString *string = [formatter stringFromNumber:[NSNumber numberWithDouble:1511511.3]];
    NSLog(@"FlyElephant:格式化之后的数字:%@",string);
    
    formatter.numberStyle = NSNumberFormatterCurrencyStyle;
    NSLog(@"FlyElephant:格式化之后的数字:%@",[formatter stringFromNumber:[NSNumber numberWithDouble:1511511.3]]);
    
    formatter.numberStyle = NSNumberFormatterPercentStyle;
    NSLog(@"FlyElephant:格式化之后的数字:%@",[formatter stringFromNumber:[NSNumber numberWithDouble:1511511.3]]);
    
    formatter.numberStyle = NSNumberFormatterScientificStyle;
    NSLog(@"FlyElephant:格式化之后的数字:%@",[formatter stringFromNumber:[NSNumber numberWithDouble:1511511.3]]);
    
    formatter.numberStyle = NSNumberFormatterSpellOutStyle;
    NSLog(@"FlyElephant:格式化之后的数字:%@",[formatter stringFromNumber:[NSNumber numberWithDouble:1511511.3]]);
    
    formatter.numberStyle = NSNumberFormatterOrdinalStyle;
    NSLog(@"FlyElephant:格式化之后的数字:%@",[formatter stringFromNumber:[NSNumber numberWithDouble:1511511.3]]);
    
    formatter.numberStyle = NSNumberFormatterCurrencyISOCodeStyle;
    NSLog(@"FlyElephant:格式化之后的数字:%@",[formatter stringFromNumber:[NSNumber numberWithDouble:1511511.3]]);
    
    formatter.numberStyle = NSNumberFormatterCurrencyPluralStyle;
    NSLog(@"FlyElephant:格式化之后的数字:%@",[formatter stringFromNumber:[NSNumber numberWithDouble:1511511.3]]);
    
    formatter.numberStyle = NSNumberFormatterCurrencyAccountingStyle;
    NSLog(@"FlyElephant:格式化之后的数字:%@",[formatter stringFromNumber:[NSNumber numberWithDouble:1511511.3]]);
}

测试效果:


iOS-NSNumberFormatter_第1张图片
FlyElephant.png

自定义格式

关于钱的显示需要精确到具体的位数,已经百分比需要精确的位数,单纯的设置type是无法实现的,需要设置不同的formate:

    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setPositiveFormat:@"###,##0.###"];
    NSString *formattedNumberString = [numberFormatter stringFromNumber:[NSNumber numberWithFloat:8971511511.34567]];
    NSLog(@"金钱自定义格式化:formattedNumberString: %@", formattedNumberString);
    
    NSNumberFormatter *percentFormatter = [[NSNumberFormatter alloc] init];
    [percentFormatter setNumberStyle:NSNumberFormatterPercentStyle];
    [percentFormatter setPositiveFormat:@"0.00%;-0.00%"];
    NSLog(@"百分比自定义格式化:formattedNumberString: %@",[percentFormatter stringFromNumber:[NSNumber numberWithDouble:0.9899]]);

你可能感兴趣的:(iOS-NSNumberFormatter)