通過NSNumberFormatter披坏,設(shè)置NSNumber輸出的格式
NSNumberFormatter *formatter = [[NSNumberFormatteralloc]init];
formatter.numberStyle =NSNumberFormatterDecimalStyle;
NSString *newAmount = [formatterstringFromNumber: [NSNumbernumberWithint:123456789]];
NSNumberFormatter類有個(gè)屬性numberStyle原献,是一個(gè)枚舉型,設(shè)置不同的值可以輸出不同的數(shù)字格式埂淮。枚舉包括:
typedef CF_ENUM(CFIndex, CFNumberFormatterStyle) { // number format styles
kCFNumberFormatterNoStyle = 0,
kCFNumberFormatterDecimalStyle = 1,
kCFNumberFormatterCurrencyStyle = 2,
kCFNumberFormatterPercentStyle = 3,
kCFNumberFormatterScientificStyle = 4,
kCFNumberFormatterSpellOutStyle = 5,
kCFNumberFormatterOrdinalStyle CF_ENUM_AVAILABLE(10_11, 9_0) = 6,
kCFNumberFormatterCurrencyISOCodeStyle CF_ENUM_AVAILABLE(10_11, 9_0) = 8,
kCFNumberFormatterCurrencyPluralStyle CF_ENUM_AVAILABLE(10_11, 9_0) = 9,
kCFNumberFormatterCurrencyAccountingStyle CF_ENUM_AVAILABLE(10_11, 9_0) = 10,
};
各個(gè)枚舉對(duì)應(yīng)輸出數(shù)字格式的效果如下:
123456789
123,456,789
¥123,456,789.00
-123,456,789%
1.23456789E8
一億二千三百四十五萬六千七百八十九
第1,2345,6789
CNY123,456,789.00
123,456,789.00人民幣
¥123,456,789.00