objective-c 中資料類型之五 數值類(NSNumber及NSNumberFormatter)

來源:互聯網
上載者:User

標籤:ios開發   nsnumber   

// 聊聊NSNumber, NSNumber可以封裝c中基礎資料型別 (Elementary Data Type) 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;        // 既然NSNumber能封裝BOOL,NSInteger,CGFloat等基礎資料格式,那如何判斷NSNumber是什麼基礎類型呢,接著看    /*     + (NSNumber *)numberWithChar:(char)value;     + (NSNumber *)numberWithUnsignedChar:(unsigned char)value;     + (NSNumber *)numberWithShort:(short)value;     + (NSNumber *)numberWithUnsignedShort:(unsigned short)value;     + (NSNumber *)numberWithInt:(int)value;     + (NSNumber *)numberWithUnsignedInt:(unsigned int)value;     + (NSNumber *)numberWithLong:(long)value;     + (NSNumber *)numberWithUnsignedLong:(unsigned long)value;     + (NSNumber *)numberWithLongLong:(long long)value;     + (NSNumber *)numberWithUnsignedLongLong:(unsigned long long)value;     + (NSNumber *)numberWithFloat:(float)value;     + (NSNumber *)numberWithDouble:(double)value;     + (NSNumber *)numberWithBool:(BOOL)value;     + (NSNumber *)numberWithInteger:(NSInteger)value NS_AVAILABLE(10_5, 2_0);     + (NSNumber *)numberWithUnsignedInteger:(NSUInteger)value NS_AVAILABLE(10_5, 2_0);     */    num = [NSNumber numberWithChar:'a'];    NSLog(@"%@ %@", NSStringFromClass(num.class), @(num.objCType)); // __NSCFNumber c    num = [NSNumber numberWithUnsignedChar:'a'];    NSLog(@"%@ %@", NSStringFromClass(num.class), @(num.objCType)); // __NSCFNumber s    num = [NSNumber numberWithShort:97];    NSLog(@"%@ %@", NSStringFromClass(num.class), @(num.objCType)); // __NSCFNumber s    num = [NSNumber numberWithUnsignedShort:97];    NSLog(@"%@ %@", NSStringFromClass(num.class), @(num.objCType)); // __NSCFNumber i    num = [NSNumber numberWithInt:97];    NSLog(@"%@ %@", NSStringFromClass(num.class), @(num.objCType)); // __NSCFNumber i    num = [NSNumber numberWithUnsignedInt:97];    NSLog(@"%@ %@", NSStringFromClass(num.class), @(num.objCType)); // __NSCFNumber q    num = [NSNumber numberWithLong:97];    NSLog(@"%@ %@", NSStringFromClass(num.class), @(num.objCType)); // __NSCFNumber i    num = [NSNumber numberWithUnsignedLong:97];    NSLog(@"%@ %@", NSStringFromClass(num.class), @(num.objCType)); // __NSCFNumber q    num = [NSNumber numberWithLongLong:97];    NSLog(@"%@ %@", NSStringFromClass(num.class), @(num.objCType)); // __NSCFNumber q    num = [NSNumber numberWithUnsignedLongLong:97];    NSLog(@"%@ %@", NSStringFromClass(num.class), @(num.objCType)); // __NSCFNumber q    num = [NSNumber numberWithFloat:1.2];    NSLog(@"%@ %@", NSStringFromClass(num.class), @(num.objCType)); // __NSCFNumber f    num = [NSNumber numberWithDouble:1.2];    NSLog(@"%@ %@", NSStringFromClass(num.class), @(num.objCType)); // __NSCFNumber d    num = [NSNumber numberWithBool:YES];    NSLog(@"%@ %@", NSStringFromClass(num.class), @(num.objCType)); // __NSCFBoolean c    num = [NSNumber numberWithInteger:97];    NSLog(@"%@ %@", NSStringFromClass(num.class), @(num.objCType)); // __NSCFNumber i    num = [NSNumber numberWithUnsignedInteger:YES];    NSLog(@"%@ %@", NSStringFromClass(num.class), @(num.objCType)); // __NSCFNumber i        // 這樣就看出來了,判斷整型int類,包括short, unsigned short, int, unsigned int, long, unsigned long, long long, unsigned long long,就這樣判斷    NSArray *intTypeArray = @[@"s", @"i", @"q"];    NSString *intType = [NSString stringWithFormat:@"%s", num.objCType];    if ([NSStringFromClass(num.class) isEqualToString:@"__NSCFNumber"] && [intTypeArray containsObject:intType]) {        NSLog(@"%@是一個整數", num);    }    // 浮點型判斷    NSArray *floatTypeArray = @[@"f", @"d"];    <pre name="code" class="objc">    NSString *floatType = [NSString stringWithFormat:@"%s", num.objCType];    if ([NSStringFromClass(num.class) isEqualToString:@"__NSCFNumber"] && [floatTypeArray containsObject:floatType]) {        NSLog(@"%@是一個浮點型", num);    }    // char判斷    NSArray *charTypeArray = @[@"c"];    NSString *charType = [NSString stringWithFormat:@"%s", num.objCType];    if ([NSStringFromClass(num.class) isEqualToString:@"__NSCFNumber"] && [charTypeArray containsObject:charType]) {        NSLog(@"%@是一個字元型", num);    }    // BOOL判斷    if ([NSStringFromClass(num.class) isEqualToString:@"__NSCFBoolean"]) {        NSLog(@"%@是一個字元型", num);    }    if ([num isKindOfClass:NSClassFromString(@"__NSCFBoolean")]) {        NSLog(@"num is BOOL type");    } else {        NSLog(@"num is not BOOL type");    }    // 下面聊NSNumberFormatter    // 從學號談起,一般學號的全格式是 入學年份(前兩位)+大學(學院)代碼+專業代碼+班級+個人班級學號 05+01+02+1+01,給班級學生列成績單,知道學生班級學號,產生學校學號,如5號,應該產生050102105,我們想到的方法是c中的格式化,例如:    NSInteger classCode = 5;    NSString *schoolStuCode = [NSString stringWithFormat:@"0501021%2.2d", classCode];    NSLog(@"schoolStuCode = %@", schoolStuCode); // 050102105        // 另一種方法是NSNumberFormatter轉換,上代碼    // 資料格式轉換類    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];        numberFormatter.numberStyle = kCFNumberFormatterNoStyle;    numberFormatter.formatWidth = 2;    // 資料寬度為2,不足前面補0    numberFormatter.paddingCharacter = @"0";    schoolStuCode = [NSString stringWithFormat:@"0501021%@", [numberFormatter stringFromNumber:@(classCode)]];    NSLog(@"schoolStuCode = %@", schoolStuCode); // 050102105    // 這裡也輸出了050102105        // 之後體驗NSNumberFormatter強大功能    // 字串分隔,可用於手機號顯示    NSString *formatterStr;    numberFormatter.numberStyle = kCFNumberFormatterDecimalStyle;    formatterStr = [numberFormatter stringFromNumber:@102140302];    NSLog(@"%@", formatterStr); // 102,140,302    // 從格式字串轉換為資料,其他類似    NSNumber *number = [numberFormatter numberFromString:formatterStr];    NSLog(@"number = %@", number);    // 貨幣符號    numberFormatter.numberStyle = NSNumberFormatterCurrencyStyle;    formatterStr = [numberFormatter stringFromNumber:@1.2];    NSLog(@"%@", formatterStr); // $1.20    // 百分數格式    numberFormatter.numberStyle = kCFNumberFormatterPercentStyle;    formatterStr = [numberFormatter stringFromNumber:@1.2];    NSLog(@"%@", formatterStr); // 120%    // 科學計數格式    numberFormatter.numberStyle = kCFNumberFormatterScientificStyle;    formatterStr = [numberFormatter stringFromNumber:@1.2];    NSLog(@"%@", formatterStr); // 1.2E0    // 口語格式    numberFormatter.numberStyle = kCFNumberFormatterSpellOutStyle;    formatterStr = [numberFormatter stringFromNumber:@1.2];    NSLog(@"%@", formatterStr); // one point two


objective-c 中資料類型之五 數值類(NSNumber及NSNumberFormatter)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.