IOS number Processing (int-->nsnumber,nsnumber-->nsinteger,string-->double,cgfloat--Dobule)

Source: Internet
Author: User

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

  1. NSLog (@"The size of an int. is:%lu bytes.",sizeof(int));
  2. NSLog (@"The size of a short int are:%lu bytes.",sizeof(short int  ));
  3. NSLog (@"The size of a long int is:%lu bytes.",sizeof(long int));
  4. NSLog (@"The size of a char is:%lu bytes.",sizeof(char));
  5. NSLog (@"The size of a float is:%lu bytes.",sizeof(float));
  6. NSLog (@"The size of a double is:%lu bytes.",sizeof(double));
  7. NSLog (@"the size of a bool is:%lu bytes.",sizeof(bool)); Additional setup after loading the view,

Results:

  1. 2012-06-13 13:55:46.726 basetype[3032:f803] The size of an int is:4 bytes.
  2. 2012-06-13 13:55:46.726 basetype[3032:f803] the size of a short int is:2 bytes.
  3. 2012-06-13 13:55:46.727 basetype[3032:f803] The size of a long int is:4 bytes.
  4. 2012-06-13 13:55:46.731 basetype[3032:f803] The size of a char is:1 bytes.
  5. 2012-06-13 13:55:46.732 basetype[3032:f803] The size of a float is:4 bytes.
  6. 2012-06-13 13:55:46.733 basetype[3032:f803] The size of a double is:8 bytes.
  7. 2012-06-13 13:55:46.733 basetype[3032:f803] The size of a bool is:1 bytes.

3. Formatted output data

  1. //integral type
  2. int integertype = 5;
  3. //floating-point type
  4. float floattype = 3.1415;
  5. //double floating-point type
  6. double doubletype = 2.2033;
  7. //short-integer
  8. Short int shorttype = 200;
  9. //long-integer
  10. long long int longlongtype = 7758123456767L;
  11. //c Language strings
  12. Char * cstring = "This is a string!";
  13. //integral type
  14. NSLog (@"The value of Integertype =%d", Integertype);
  15. //floating-point type
  16. NSLog (@"The value of Floattype =%.2f", Floattype);
  17. //double floating-point type
  18. NSLog (@"The value of Doubletype =%e", Doubletype);
  19. //short-integer
  20. NSLog (@"The value of Shorttype =%hi", Shorttype);
  21. //long-integer
  22. NSLog (@"The value of Longlongtype =%lli", Longlongtype);
  23. //c Language strings
  24. NSLog (@"The value of CString =%s", CString);

Results:

    1. 2012-06-13 14:06:18.757 basetype[3215:f803] the value  of integertype = 5  
    2. 2012-06-13 14:06:18.757 basetype[3215: f803] the value of floattype = 3.14  
    3. 2012-06-13  14:06:18.758 basetype[3215:f803] the value of doubletype = 2.203300e+ 00  
    4. 2012-06-13 14:06:18.758 basetype[3215:f803] the value of  shorttype = 200  
    5. 2012-06-13 14:06:18.758 basetype[3215:f803 ] the value of longlongtype = 7758123456767  
    6. 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:

    1. Nsmutablearray *array = [[Nsmutablearray alloc]init];
    2. [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:

    1. NSNumber *num = [NSNumber numberwithint:88];
    2. Nsinteger integer = [num intvalue];

5. Conversion between NSString and Nsinteger

    1. Nsinteger integernumber = 888;
    2. NSString * string = [NSString stringwithformat:@"%d", Integernumber];
    3. NSLog (@"string is%@", String);
    1. Integer = [string Intvalue];
    2. 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.