金额千分位格式化

2017-09-06  本文已影响81人  LiwaySun

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

NSNumberFormatter *formatter = [[NSNumber Formatteralloc] init];

formatter.numberStyle =NSNumberFormatterDecimalStyle;

NSString *newAmount = [formatter stringFromNumber:[NSNumber numberWithint:123456789]];

(注意传入参数的数据长度,还可用double类型等)

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

enum{

NSNumberFormatterNoStyle = kCFNumberFormatterNoStyle,  

NSNumberFormatterDecimalStyle = kCFNumberFormatterDecimalStyle,

NSNumberFormatterCurrencyStyle = kCFNumberFormatterCurrencyStyle,

NSNumberFormatterPercentStyle = kCFNumberFormatterPercentStyle,

NSNumberFormatterScientificStyle = kCFNumberFormatterScientificStyle,

NSNumberFormatterSpellOutStyle = kCFNumberFormatterSpellOutStyle

};

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

123456789

123,456,789

¥123,456,789.00

-539,222,988%

1.23456789E8

一亿二千三百四十五万六千七百八十九

上一篇下一篇

猜你喜欢

热点阅读