IOS number processing (INT-> nsnumber, nsnumber-> nsinteger, string-> double, cgfloat &

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, because you do not need to consider whether the device is 32-bit or 64-bit.

6) nsinteger is a basic type, but nsnumber is a class. If you want to store a value in nsmutablearray, you cannot directly use nsinteger, for example, in an nsmutablearray.

7) nsstring and nsinteger Conversion

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", );

// Use the cgfloat and nsdecimalnumber objects to process floating point values:

Nsdecimalnumber * mydecimalobj = [[nsdecimalnumber alloc] initwithstring: @ "23.30"];

Nslog (@ "mydecimalobj doublevalue = % 6.3f", [mydecimalobj doublevalue]);

Cgfloat mycgfloatvalues = 43.4;

Nsdecimalnumber * myotherdecimalobj = [[nsdecimalnumber alloc] initwithfloat: mycgfloatvalue];

Nslog (@ "mytherdecimalobj doublevalue = % 6.5f", [mytherdecimalobj doublevalue]);

}

 

2. Length of basic data types in C Language

  1. Nslog (@ "the size of an int is: % lu bytes .",Sizeof(Int));
  2. Nslog (@ "the size of a short Int Is: % 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); // Do any additional setup after loading the view,

Result:

  1. 13:55:46. 726 basetype [3032: f803] the size ofIntIs: 4 bytes.
  2. 13:55:46. 726 basetype [3032: f803] the size ofShort IntIs: 2 bytes.
  3. 13:55:46. 727 basetype [3032: f803] the size ofLong IntIs: 4 bytes.
  4. 13:55:46. 731 basetype [3032: f803] the size ofCharIs: 1 bytes.
  5. 13:55:46. 732 basetype [3032: f803] the size ofFloatIs: 4 bytes.
  6. 13:55:46. 733 basetype [3032: f803] the size ofDoubleIs: 8 bytes.
  7. 13:55:46. 733 basetype [3032: f803] the size ofBoolIs: 1 bytes.

3. Format output data

  1. // Integer
  2. IntIntegertype = 5;
  3. // Floating point type
  4. FloatFloattypes = 3.1415;
  5. // Double Floating Point
  6. DoubleDoubletype = 2.2033;
  7. // Short integer
  8. Short IntStruct type = 200;
  9. // Long integer
  10. Long Long IntLonglongtype = 7758123456767l;
  11. // C language string
  12. Char* Cstring = "this is a string! ";
  13. // Integer
  14. Nslog (@ "the value of integertype = % d", integertype );
  15. // Floating point type
  16. Nslog (@ "the value of floattype = %. 2f", floattype );
  17. // Double Floating Point
  18. Nslog (@ "the value of doubletype = % E", doubletype );
  19. // Short integer
  20. Nslog (@ "the value of parameter type = % Hi", parameter type );
  21. // Long integer
  22. Nslog (@ "the value of longlongtype = % llI", longlonglongtype );
  23. // C language string
  24. Nslog (@ "the value of cstring = % s", cstring );

 

Result:

  1. 14:06:18. 757 basetype [3215: f803] the value of integertype = 5
  2. 14:06:18. 757 basetype [3215: f803] the value of floattype = 3.14
  3. 14:06:18. 758 basetype [3215: f803] the value of doubletype = 2.203300e + 00
  4. 14:06:18. 758 basetype [3215: f803] the value of Role type = 200
  5. 14:06:18. 758 basetype [3215: f803] the value of longlongtype = 7758123456767
  6. 14:06:18. 758 basetype [3215: f803] the value of cstring =ThisIs a string!

 

4. Int, nsinteger, nsuinteger, nsnumber
1. when int type variables are required, you can use Int or nsinteger, just like a program that writes C. But nsinteger is recommended, in this case, you do not need to consider whether the device is 32-bit or 64-bit.
2. nsuinteger is unsigned, that is, there is no negative number, and nsinteger is signed.

3. Some people say that since nsinteger and other basic types are available, why does nsnumber exist? Of course, their functions are different.
Nsinteger is a basic type, but nsnumber is a class. If you want to store a value in nsmutablearray, you cannot directly use nsinteger, for example, in an nsmutablearray:

  1. Nsmutablearray * array = [[nsmutablearray alloc] init];
  2. [Array addobject: [nsnumber numberwithint: 88];

 

This will cause a compilation error, because nsmutablearray requires a class, but '88' is not a class.

Cocoa provides the nsnumber class for packaging (that is, implemented in the form of objects) Basic data types.
For example:
+ (Nsnumber *) numberwithchar: (char) value;
+ (Nsnumber *) numberwithint: (INT) value;
+ (Nsnumber *) numberwithfloat: (float) value;
+ (Nsnumber *) numberwithbool: (bool) value;

After encapsulating the basic data type into nsnumber, you can obtain it again 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. nsstring and nsinteger Conversion

  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.

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.