基本數據類型的相互轉換 int float NSinteger double 的相互轉化


#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        int a=6;
        float b=7.8;
        double d=5.6;
        NSInteger c=9;
        //intWithInt實例方法:將int類型包裝成一個NSNumber對象
        NSNumber *monthNum=[[NSNumber alloc]initWithInt:a];
        //numberWithFloat類方法:將float類型包裝成一個NUNumber對象
        NSNumber *lenthNum=[NSNumber numberWithFloat:b];
        NSNumber *dd=[NSNumber numberWithDouble:d];
        NSLog(@"monthNum=%@,lenthNum=%@",monthNum,lenthNum);
        //轉換,將monthNum從NSNumber強制轉換為int類型
        int month=[monthNum intValue];
        float lenth=[lenthNum floatValue];
        //轉換,將dd從NSNumber強制轉換為int類型
        int intd=[dd intValue];
        NSLog(@"month=%d,lenth=%f,intd=%d",month,lenth,intd);
        //使用的是64位Xcode編譯器,所以NSInteger是typedef long NSInteger,輸出時應該是%ld
        NSLog(@"c=%ld",c);
    }
    return 0;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM