【iOS开发】金额数字的格式化 NSNumberFormatter

96
文刂Rn
2017.02.27 16:22* 字数 151

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

enum {
    NSNumberFormatterNoStyle = kCFNumberFormatterNoStyle, //无格式,四舍五入,原值2.7999999999,直接输出3 
    NSNumberFormatterDecimalStyle = kCFNumberFormatterDecimalStyle, //小数型,保留小数输出2.8
    NSNumberFormatterCurrencyStyle = kCFNumberFormatterCurrencyStyle, //货币型,加上了人民币标志,原值输出¥2.8
    NSNumberFormatter
    NSNumberFormatterPercentStyle = kCFNumberFormatterPercentStyle,  //百分比型,本身数值乘以100后用百分号表示,输出280%
    NSNumberFormatterScientificStyle = kCFNumberFormatterScientificStyle, //科学计数型,原值表示,输出2.799999999E0
    NSNumberFormatterSpellOutStyle = kCFNumberFormatterSpellOutStyle  //全拼,原值的中文表示,输出二点七九九九...
};

typedef NSUInteger NSNumberFormatterStyle;

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

[1243:403] Formatted number string:123456789
[1243:403] Formatted number string:123,456,789
[1243:403] Formatted number string:¥123,456,789.00
[1243:403] Formatted number string:-539,222,988%
[1243:403] Formatted number string:1.23456789E8
[1243:403] Formatted number string:一亿二千三百四十五万六千七百八十九

其中第三项和最后一项的输出会根据系统设置的语言区域的不同而不同

**
 *
 *  格式化余额数据
 *
 *  @param string 余额数据字符串
 *
 *  @return 带逗号余额
 */
+ (NSString *)stringToDealMoneyString:(NSString *)string {

    NSNumberFormatter *numFormatter = [[NSNumberFormatter alloc] init];
    [numFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
    NSNumberFormatter *numFormatter2 = [[NSNumberFormatter alloc] init];
    [numFormatter2 setNumberStyle:NSNumberFormatterNoStyle];
    NSNumber *num = [numFormatter2 numberFromString:string];
    NSString *tempStr = [numFormatter stringFromNumber:num];
    NSString *balanceStr = [tempStr substringFromIndex:1];
    if ([tempStr hasPrefix:@"-"]) {
        balanceStr = [NSString stringWithFormat:@"-%@",[tempStr substringFromIndex:2]];
    }
    return balanceStr;

}

保留两位小数不四舍五入

NSNumber *number = @(99.999);
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.roundingMode = NSNumberFormatterRoundFloor;
formatter.maximumFractionDigits = 2;

NSLog(@"%@", [formatter stringFromNumber:number]); //99.99
float num = 99.999;
NSLog(@"---%.2f", num);     //---100.00

// ==================== 定制 ====================

    NSNumberFormatter *numberFormatter = [NSNumberFormatter new];

    numberFormatter.numberStyle = NSNumberFormatterDecimalStyle;

    // 格式宽度
    numberFormatter.formatWidth = 15;

    // 填充符
    numberFormatter.paddingCharacter = @"?";

    // 填充位置
    numberFormatter.paddingPosition = kCFNumberFormatterPadBeforeSuffix;
    numberFormatter.positiveSuffix = @"元";

    NSLog(@"%@",[numberFormatter numberFromString:@"10000000元"]);  // 10000000

    // 缩放因子,你可以将一个数缩放指定比例,然后给其添加后缀,如传入一个3000,你希望表示为3千,就要用到这个属性
    // 防止影响后面的测试,故注掉
//    numberFormatter.multiplier = @1000;

//    NSLog(@"%@千",[numberFormatter numberFromString:@"1000"]);  // 1千

//    numberFormatter.multiplier     = @0.001;
//    numberFormatter.positiveSuffix = @"千";
//    NSLog(@"%@",[numberFormatter stringFromNumber:@10000]);    // 10千

    // 机制不明确,负号,正号
//    numberFormatter.negativeFormat = @"^";
//    numberFormatter.positiveFormat = @"~0";

    // 貌似没什么用
    numberFormatter.allowsFloats = NO;
    numberFormatter.alwaysShowsDecimalSeparator = NO;
    numberFormatter.maximum = @1000;
    numberFormatter.minimum = @100;

    // 小数点样式
    numberFormatter.decimalSeparator = @".";

    // 零的样式
    numberFormatter.zeroSymbol       = @"-";

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

    // 指定符号,与我们在前面类方法中说明的一致
    NSLog(@"货币代码%@",numberFormatter.currencyCode);                     // 货币代码USD
    NSLog(@"货币符号%@",numberFormatter.currencySymbol);                   // 货币符号$
    NSLog(@"国际货币符号%@",numberFormatter.internationalCurrencySymbol);   // 国际货币符号USD
    NSLog(@"百分比符号%@",numberFormatter.percentSymbol);                   // 百分比符号%
    NSLog(@"千分号符号%@",numberFormatter.perMillSymbol);                   // 千分号符号‰
    NSLog(@"减号符号%@",numberFormatter.minusSign);                         // 减号符号-
    NSLog(@"加号符号%@",numberFormatter.plusSign);                          // 加号符号+
    NSLog(@"指数符号%@",numberFormatter.exponentSymbol);                    // 指数符号E

    // 整数最多位数
    numberFormatter.maximumIntegerDigits = 10;

    // 整数最少位数
    numberFormatter.minimumIntegerDigits = 2;

    // 小数位最多位数
    numberFormatter.maximumFractionDigits = 3;

    // 小数位最少位数
    numberFormatter.minimumFractionDigits = 1;

    // 数字分割的尺寸
    numberFormatter.groupingSize = 4;

    // 除了groupingSize决定的尺寸外,其他数字位分割的尺寸
    numberFormatter.secondaryGroupingSize = 2;

    // 最大有效数字个数
    numberFormatter.maximumSignificantDigits = 12;

    // 最少有效数字个数
    numberFormatter.minimumSignificantDigits = 3;

    NSLog(@"正数%@,负数%@",[numberFormatter stringFromNumber:@(+12135230.2346)],[numberFormatter stringFromNumber:@(-12135231.2346)]);  // 正数!12,13,5230.2346元,负数@12,13,5231.2346亏
    NSLog(@"零 = %@",[numberFormatter stringFromNumber:@(0)]); //  零 = -

    // 舍入值,比如以10为进位值,那么156就进位为160,154进位为150
    numberFormatter.roundingIncrement = @10;

    // 舍入方式
    numberFormatter.roundingMode = kCFNumberFormatterRoundHalfUp;
    NSLog(@"%@",[numberFormatter stringFromNumber:@123456.7890]);  // !12,3460元
iOS之朝花夕拾
Web note ad 1