IOS学习 关于NSNumberFormatter,NSNumber转NSString的小细节

NSNumberFormatter *formatter = [NSNumberFormatter alloc] init] autorelease];

    NSNumber *number = [NSNumber numberWithDouble:1234.99];

    [formatter setNumberStyle:NSNumberFormatterDecimalStyle];

    NSLog(@"Decimal Style: %@", [formatter stringFromNumber:number]);

   

    number = [NSNumber numberWithDouble:.2];

    [formatter setNumberStyle:NSNumberFormatterPercentStyle];

    NSLog(@"Percent Style: %@", [formatter stringFromNumber:number]);

   

    number = [NSNumber numberWithDouble:200.95];

    [formatter setNumberStyle:NSNumberFormatterCurrencyStyle];

    NSLog(@"Currency Style: %@", [formatter stringFromNumber:number]);

   

    [formatter setNumberStyle:NSNumberFormatterScientificStyle];

    NSLog(@"Scientific Style: %@", [formatter stringFromNumber:number]);

   

    [formatter setNumberStyle:NSNumberFormatterSpellOutStyle];

    NSLog(@"Spelled Out Style: %@", [formatter stringFromNumber:number]);

   

    NSLocale *locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"zh_Hans"] autorelease];

    [formatter setLocale:locale];

    NSLog(@"Spelled Out Style(locale): %@", [formatter stringFromNumber:number]);

 

Console:

Decimal Style: 1,234.99

Percent Style: 20%

Currency Style: $200.95

Scientific Style: 2.0095E2

Spelled Out Style: two hundred point nine five

Spelled Out Style(locale): 二百点九五

备注:默认是0为整数类型

你可能感兴趣的:(IOS学习 关于NSNumberFormatter,NSNumber转NSString的小细节)