iOS 金额千位分显示

通过NSNumberFormatter,设置NSNumber输出的格式

NSNumberFormatter *formatter = [[NSNumberFormatteralloc]init];
formatter.numberStyle =NSNumberFormatterDecimalStyle;
NSString *newAmount = [formatterstringFromNumber: [NSNumbernumberWithint:123456789]];

NSNumberFormatter类有个属性numberStyle,是一个枚举型,设置不同的值可以输出不同的数字格式。枚举包括:

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,
};

各个枚举对应输出数字格式的效果如下:

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

你可能感兴趣的:(iOS 金额千位分显示)