NSNumberFormatter 常用方法

======================================

            NSNumberFormatter  常用方法

======================================

====================================================

NSNumber *number = @(12342323.55665);

NSNumberFormatter *formatter =[[NSNumberFormatter alloc]init];

// 指定货币样式

formatter.numberStyle = NSNumberFormatterDecimalStyle;

// 自定义样式

[formatter setPositiveFormat:@"###0.###"];

// 数字之间的分隔符

[formatter setDecimalSeparator:@":"];

[formatter setCurrencyDecimalSeparator:@","];

// 前缀符号

formatter.positivePrefix = @"!";

// 后缀符号

formatter.positiveSuffix = @"元";

// 间隙中添加的符号

formatter.paddingCharacter = @"?";

// 格式的宽度

formatter.formatWidth = 15;

// 最少展示两位小数

formatter.minimumFractionDigits = 2;

// 最多展示三位整数

formatter.maximumIntegerDigits = 5;

NSString *numStr =[formatter stringFromNumber:number];

// ??????!323:557元

====================================================

    NSNumber *num = @0.655521;

    NSNumberFormatter *formatter4 =[[NSNumberFormatter alloc]init];

    formatter4.numberStyle = kCFNumberFormatterPercentStyle;

    //以百分制形式输出  整个数字乘以保留2为小数,第三位小数四舍五入,然后乘以100,同时在最后加上百分号

    formatter4.minimumFractionDigits = 2;

    NSLog(@"kCFNumberFormatterPercentStyle=%@",[formatter4 stringFromNumber:num]);

   NSNumber *number1 = @123456789;

    NSNumber *number2 = @123.456789;

    NSNumber *number3 = @(YES);

    NSString *str1 =[NSNumberFormatter localizedStringFromNumber:number1

                                                      numberStyle:NSNumberFormatterDecimalStyle];

    /*

     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,

     };

     */

    NSString *str2 =[numberFormatter stringFromNumber:number2];

    NSNumber *number4 =[numberFormatter numberFromString:str1];

    NSNumber *number = @123456789.123456789;

    NSNumberFormatter *numberFormatter =[[NSNumberFormatter alloc]init];

    numberFormatter.numberStyle = kCFNumberFormatterNoStyle;//

    numberFormatter.positiveFormat = @"-¤#,##0.00";//类似dateFormatter

    numberFormatter.formatWidth = 20;//数据宽度为20,

    numberFormatter.paddingCharacter = @"a";//不足前面补a

    numberFormatter.locale =[NSLocale currentLocale];//本地化

    numberFormatter.generatesDecimalNumbers = NO;//默认NO,YES-转换成NSNumber方法会转换成NSDecimalNumber

    //    if([[numberFormatter numberFromString:str]isKindOfClass:[NSDecimalNumber class]]){

    //        NSLog(@"NSDecimalNumber");

    //    } else if([[numberFormatter numberFromString:str]isKindOfClass:[NSNumber class]]){

    //        NSLog(@"NSNumber");

    //    }

    //    NSDecimalNumber *dnumber =(NSDecimalNumber *)[numberFormatter numberFromString:str];

    NSString *str =[numberFormatter stringFromNumber:number];   

    NSLog(@"%@",str);

你可能感兴趣的:(NSNumberFormatter 常用方法)