iOS 数字钱数格式化

可以通过NSNumberFormatter,设置NSNumber输出的格式
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];

    numberFormatter.numberStyle = NSNumberFormatterDecimalStyle;

NSNumberFormatterStyle 选项如下
/*
typedef NS_ENUM(NSUInteger, NSNumberFormatterStyle) {
NSNumberFormatterNoStyle = kCFNumberFormatterNoStyle,四舍五入
NSNumberFormatterDecimalStyle = kCFNumberFormatterDecimalStyle,金额 100,200,300.123
NSNumberFormatterCurrencyStyle = kCFNumberFormatterCurrencyStyle,货币 $100,200,300.12
NSNumberFormatterPercentStyle = kCFNumberFormatterPercentStyle,百分比 12%
NSNumberFormatterScientificStyle = kCFNumberFormatterScientificStyle,科学计数法 1.234E8
NSNumberFormatterSpellOutStyle = kCFNumberFormatterSpellOutStyle,口语 One...
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,
};
*/

    numberFormatter.groupingSeparator = @" "; //数字分割的格式
    numberFormatter.groupingSize = 3; // 数字分割的尺寸
    numberFormatter.maximumFractionDigits = 2; //小数位最多位数
    numberFormatter.maximumIntegerDigits = 10;// 整数最多位数
    numberFormatter.minimumIntegerDigits = 2;    // 整数最少位数
    numberFormatter.maximumFractionDigits = 3;    // 小数位最多位数
// 最大有效数字个数
numberFormatter.maximumSignificantDigits = 12;
// 最少有效数字个数
numberFormatter.minimumSignificantDigits = 3;

// 前缀和后缀
numberFormatter.positivePrefix = @"¥";
numberFormatter.positiveSuffix = @"元";
numberFormatter.negativePrefix = @"¥";
numberFormatter.negativeSuffix = @"元";

你可能感兴趣的:(iOS 数字钱数格式化)