美文网首页
数字Number

数字Number

作者: 阿龍飛 | 来源:发表于2016-08-02 15:04 被阅读50次

OC中小数的精确计算http://www.tuicool.com/articles/QzMFZf

 NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    
 formatter.numberStyle = kCFNumberFormatterDecimalStyle;
    
 NSString *string = [formatter stringFromNumber:@123456789.89];
    
 NSLog(@"%@",string);

kCFNumberFormatterDecimalStyle
123,456,789.89
kCFNumberFormatterCurrencyStyle
¥ 123,456,789.89
kCFNumberFormatterPercentStyle
12,345,678,989%
kCFNumberFormatterScientificStyle
1.2345678989E8
kCFNumberFormatterSpellOutStyle
一亿二千三百四十五万六千七百八十九点八九
kCFNumberFormatterOrdinalStyle
第1,2345,6790



输出005

  NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    NSInteger classCode = 5;
    numberFormatter.numberStyle = kCFNumberFormatterNoStyle;
    numberFormatter.formatWidth = 3;    // 数据宽度为3,不足前面补0
    numberFormatter.paddingCharacter = @"0";
    NSString * schoolStuCode = [NSString stringWithFormat:@"%@", [numberFormatter stringFromNumber:@(classCode)]];
    NSLog(@"schoolStuCode = %@", schoolStuCode); // 005

输出005

NSInteger classCode = 5;
    NSString *schoolStuCode = [NSString stringWithFormat:@"%3.3d", classCode];
    NSLog(@"schoolStuCode = %@", schoolStuCode); // 005

聊聊NSNumber, NSNumber可以封装c中基本数据类型 char,int,long,float,BOOL,NSInteger,CGFloat等。

  NSNumber *num = [NSNumber numberWithChar:'a'];
  NSLog(@"%@", num); // 97
  
  num = [NSNumber numberWithInteger:97];
  num = [NSNumber numberWithFloat:97.0];
  num = [NSNumber numberWithBool:YES];
  
  // 不过直接用下面的方式封装会更方便
  num = @'a';
  num = @97;
  num = @97.0;
  num = @YES;

相关文章

网友评论

      本文标题:数字Number

      本文链接:https://www.haomeiwen.com/subject/fzifsttx.html