Created
June 9, 2013 07:23
-
-
Save katsuhide/5742186 to your computer and use it in GitHub Desktop.
NSNumber周り
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// short型→NSNumber型 | |
NSNumber *num = [NSNumber numberWithShort:32767]; | |
// int型→NSNumber型 | |
NSNumber *num = [NSNumber numberWithInt:2147483647]; | |
// long型→NSNumber型 | |
NSNumber *num = [NSNumber numberWithLong:2147483647]; | |
// float型→NSNumber型 | |
NSNumber *num = [NSNumber numberWithFloat:42.195f]; | |
// double型→NSNumber型 | |
NSNumber *num = [NSNumber numberWithDouble:3.1415f]; | |
// BOOL型→NSNumber型 | |
NSNumber *num = [NSNumber numberWithBool:YES]; | |
// char型→NSNumber型 | |
NSNumber *num = [NSNumber numberWithChar:a]; | |
// NSNumber型→int型 | |
int val = [num intValue]; | |
// NSNumber型→NSString型 | |
NSString *str = [num stringValue]; | |
// 数字を文字列に | |
float x = 1.2345f; | |
// 小数点以下2桁まで | |
NSString *str1 = [NSString stringWithFormat:@"%.2f", x]; // 1.23 | |
// 3桁 | |
NSString *str2 = [NSString stringWithFormat:@"%.3f", x]; // 1.235(四捨五入される) | |
// 5桁 | |
NSString *str3 = [NSString stringWithFormat:@"%.5f", x]; // 1.23450(0埋めされる) | |
// 桁数指定なし | |
NSString *str4 = [NSString stringWithFormat:@"%f", x]; // 1.234500(小数点以下6桁まで?) | |
double y = 1.23456789; | |
// 桁数指定なし | |
NSString *str5 = [NSString stringWithFormat:@"%f", y]; // 1.234568(小数点以下6桁まで?) | |
// e を使った表記 | |
NSString *str6 = [NSString stringWithFormat:@"%e", y]; // 1.234568e+00 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment