1 Summary:
1) int-->nsnumber:numberwithint
2) Nsnumber-->nsinteger:integervalue
3) String-->double:initwithstring
4) CGFloat--Dobule:initwithfloat,decimalobj Doublevalue
5) Use Nsinteger, as this will not have to consider whether the device is 32-bit or 64-bit.
6) Nsinteger is the underlying type, but NSNumber is a class. If you want to store a value in Nsmutablearray, it is not possible to use Nsinteger directly, such as in a nsmutablearray.
7) Mutual conversion of nsstring and Nsinteger
NSString * string = [NSString stringwithformat:@ "%d", integernumber];
Integer = [string Intvalue];
static void Numbertest () {
NSNumber *numobj = [NSNumber numberwithint:2];
NSLog (@ "numobj=%@", numobj);
Nsinteger Myinteger = [Numobj integervalue];
NSLog (@ "myinteger=%d", Myinteger);
int a = [numobj intvalue];
NSLog (@ "a=%d", a);
Floating-point values are processed using the Cgfloat,nsdecimalnumber object:
Nsdecimalnumber *mydecimalobj = [[Nsdecimalnumber alloc] initwithstring:@ "23.30"];
NSLog (@ "Mydecimalobj doublevalue=%6.3f", [Mydecimalobj Doublevalue]);
CGFloat mycgfloatvalue = 43.4;
Nsdecimalnumber *myotherdecimalobj = [[Nsdecimalnumber alloc] initwithfloat:mycgfloatvalue];
NSLog (@ "Myotherdecimalobj doublevalue=%6.5f", [Myotherdecimalobj Doublevalue]);
}
2. Basic data type length of C language
- NSLog (@"The size of an int. is:%lu bytes.",sizeof(int));
- NSLog (@"The size of a short int are:%lu bytes.",sizeof(short int ));
- NSLog (@"The size of a long int is:%lu bytes.",sizeof(long int));
- NSLog (@"The size of a char is:%lu bytes.",sizeof(char));
- NSLog (@"The size of a float is:%lu bytes.",sizeof(float));
- NSLog (@"The size of a double is:%lu bytes.",sizeof(double));
- NSLog (@"the size of a bool is:%lu bytes.",sizeof(bool)); Additional setup after loading the view,
Results:
- 2012-06-13 13:55:46.726 basetype[3032:f803] The size of an int is:4 bytes.
- 2012-06-13 13:55:46.726 basetype[3032:f803] the size of a short int is:2 bytes.
- 2012-06-13 13:55:46.727 basetype[3032:f803] The size of a long int is:4 bytes.
- 2012-06-13 13:55:46.731 basetype[3032:f803] The size of a char is:1 bytes.
- 2012-06-13 13:55:46.732 basetype[3032:f803] The size of a float is:4 bytes.
- 2012-06-13 13:55:46.733 basetype[3032:f803] The size of a double is:8 bytes.
- 2012-06-13 13:55:46.733 basetype[3032:f803] The size of a bool is:1 bytes.
3. Formatted output data
- //integral type
- int integertype = 5;
- //floating-point type
- float floattype = 3.1415;
- //double floating-point type
- double doubletype = 2.2033;
- //short-integer
- Short int shorttype = 200;
- //long-integer
- long long int longlongtype = 7758123456767L;
- //c Language strings
- Char * cstring = "This is a string!";
- //integral type
- NSLog (@"The value of Integertype =%d", Integertype);
- //floating-point type
- NSLog (@"The value of Floattype =%.2f", Floattype);
- //double floating-point type
- NSLog (@"The value of Doubletype =%e", Doubletype);
- //short-integer
- NSLog (@"The value of Shorttype =%hi", Shorttype);
- //long-integer
- NSLog (@"The value of Longlongtype =%lli", Longlongtype);
- //c Language strings
- NSLog (@"The value of CString =%s", CString);
Results:
- 2012-06-13 14:06:18.757 basetype[3215:f803] the value of integertype = 5
- 2012-06-13 14:06:18.757 basetype[3215: f803] the value of floattype = 3.14
- 2012-06-13 14:06:18.758 basetype[3215:f803] the value of doubletype = 2.203300e+ 00
- 2012-06-13 14:06:18.758 basetype[3215:f803] the value of shorttype = 200
- 2012-06-13 14:06:18.758 basetype[3215:f803 ] the value of longlongtype = 7758123456767
- 2012-06-13 14:06:18.758 basetype[3215:f803] the value of cstring = this is a string!
4, Int,nsinteger,nsuinteger,nsnumber
1. When you need to use a variable of type int, you can use int or Nsinteger as a program written C, but it is more recommended that Nsinteger be used, as this will not be considered 32-bit or 64-bit for the device.
2.NSUInteger is unsigned, that is, there are no negative numbers, and the Nsinteger is signed.
3. It is said that since all of these basic types are nsinteger, why should there be nsnumber? Their functions are, of course, different.
Nsinteger is the underlying type, but NSNumber is a class. If you want to store a value in Nsmutablearray, it is not possible to use Nsinteger directly, such as in a nsmutablearray:
- Nsmutablearray *array = [[Nsmutablearray alloc]init];
- [Array Addobject:[nsnumber numberwithint:88];
This will cause a compilation error, because the Nsmutablearray needs to be a class, but ' 88 ' is not a class.
Cocoa provides a basic data type that is packaged (that is, implemented as an object) by the NSNumber class.
For example, the following creation method:
+ (NSNumber *) Numberwithchar: (char) value;
+ (NSNumber *) Numberwithint: (int) value;
+ (NSNumber *) Numberwithfloat: (float) value;
+ (NSNumber *) Numberwithbool: (BOOL) value;
Once the base type data is encapsulated in NSNumber, it can be retrieved by using the following instance method:
-(char) charvalue;
-(int) intvalue;
-(float) floatvalue;
-(BOOL) boolvalue;
-(NSString *) stringvalue;
Example:
- NSNumber *num = [NSNumber numberwithint:88];
- Nsinteger integer = [num intvalue];
5. Conversion between NSString and Nsinteger
- Nsinteger integernumber = 888;
- NSString * string = [NSString stringwithformat:@"%d", Integernumber];
- NSLog (@"string is%@", String);
- Integer = [string Intvalue];
- NSLog (@"Integer is%d", Integernumber);
char float and other types can be converted
Http://www.cnblogs.com/csj007523/archive/2012/07/16/2593269.html
IOS number Processing (int-->nsnumber,nsnumber-->nsinteger,string-->double,cgfloat-Dobule)