1 小结:

1)int-->NSNumber:numberWithInt

2)NSNumber-->nsinteger:integerValue

3)string -->double:initWithString

4)CGFloat --> dobule:initWithFloat,decimalobj doubleValue

5)使用NSInteger,因为这样就不用考虑设备是32位的还是64位的。

6)NSInteger是基础类型,但是NSNumber是一个类。如果想要在NSMutableArray里存储一个数值,直接用NSInteger是不行的,比如在一个NSMutableArray里面.

7) NSString与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);

//浮点数值使用CGFloat,NSDecimalNumber对象进行处理:

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 、C语言的基本数据类型长度

  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,

结果:

  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、格式化输出数据

  1. //整型
  2. int integerType = 5;
  3. //浮点型
  4. float floatType = 3.1415;
  5. //双浮点型
  6. double doubleType = 2.2033;
  7. //短整型
  8. short int shortType = 200;
  9. //长整型
  10. long long int longlongType = 7758123456767L;
  11. //c语言字符串
  12. char * cstring = "this is a string!";
  13. //整型
  14. NSLog(@"The value of integerType = %d",integerType);
  15. //浮点型
  16. NSLog(@"The value of floatType = %.2f",floatType);
  17. //双浮点型
  18. NSLog(@"The value of doubleType = %e",doubleType);
  19. //短整型
  20. NSLog(@"The value of shortType = %hi",shortType);
  21. //长整型
  22. NSLog(@"The value of longlongType = %lli",longlongType);
  23. //c语言字符串
  24. NSLog(@"The value of cstring = %s",cstring);

结果:

  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.当需要使用int类型的变量的时候,可以像写C的程序一样,用int,也可以用NSInteger,但更推荐使用NSInteger,因为这样就不用考虑设备是32位的还是64位的。
2.NSUInteger是无符号的,即没有负数,NSInteger是有符号的。

 

3.有人说既然都有了NSInteger等这些基础类型了为什么还要有NSNumber?它们的功能当然是不同的。
 NSInteger是基础类型,但是NSNumber是一个类。如果想要在NSMutableArray里存储一个数值,直接用NSInteger是不行的,比如在一个NSMutableArray里面这样用:

  1. NSMutableArray *array = [[NSMutableArray alloc]init];
  2. [array addObject:[NSNumber numberWithInt:88]];

这样是会引发编译错误的,因为NSMutableArray里面放的需要是一个类,但‘88’不是类。

Cocoa提供了NSNumber类来包装(即以对象形式实现)基本数据类型。
例如以下创建方法:
+ (NSNumber *) numberWithChar: (char) value;
+ (NSNumber *) numberWithInt: (int) value;
+ (NSNumber *) numberWithFloat: (float) value;
+ (NSNumber *) numberWithBool: (BOOL) value;

将基本类型数据封装到NSNumber中后,就可以通过下面的实例方法重新获取它:
- (char) charValue;
- (int) intValue;
- (float) floatValue;
- (BOOL) boolValue;
- (NSString *) stringValue;

例子:

  1. NSNumber *num = [NSNumber numberWithInt:88];
  2. NSInteger integer = [num intValue];

5、NSString与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等类型一样可以转换

http://www.cnblogs.com/csj007523/archive/2012/07/16/2593269.html

IOS Number 处理(int-->NSNumber,NSNumber-->nsinteger,string -->double,CGFloat --> dobule)的更多相关文章

  1. iOS开发之int,NSInteger,NSUInteger,NSNumber的使用

    1.首先先了解下NSNumber类型: 苹果官方文档地址:https://developer.apple.com/library/ios/documentation/Cocoa/Reference/F ...

  2. [BS-02] iOS数组、字典、NSNumber 新写法—— @[]、@{}

    IOS数组.字典.NSNumber 新写法—— @[].@{}   //标准写法 NSNumber * number = [NSNumber numberWithInt:]; NSArray * ar ...

  3. NSNumber与NSInteger的区别 -bei

    基本类型,如同C 语言中的 int 类型一样,拿来就可以直接用. 而类在使用时,必须先创建一个对象,再为对象分配空间,接着做初始化和赋值. 类的初始化,需用类自身的方法 (类方法). 代码中所创建的对 ...

  4. NSNumber与NSInteger的区别

    Objective-C 支持的类型有两种:基本类型 和  类. 基本类型,如同C 语言中的 int 类型一样,拿来就可以直接用. 而类在使用时,必须先创建一个对象,再为对象分配空间,接着做初始化和赋值 ...

  5. iOS学习14之OC NSNumber + NSValue

    1.NSNumber 数值类. 作用:实现基本数据类型与OC对象类型的相互转化. 1> NSNumber创建对象 // 初始化方法 NSNumber *num1 = [[NSNumber all ...

  6. This sample is for changing from “float64” to “int” for values did unmarshal using map[string]interface{}. When it did unmarshal using map[string]interface{}, a number with “int” was changed to “floa

    This sample is for changing from “float64” to “int” for values did unmarshal using map[string]interf ...

  7. 【java基础学习一】int[]、Integer[]、String[] 排序( 正序、倒叙)、去重

    调用: //重复项有9.5.1.2 int[] ints = new int[]{9,4,7,8,2,5,1,6,2,5,9,1}; arrayIntTest(ints); ///////////// ...

  8. TypeError: Fetch argument 0 has invalid type <type 'int'>, must be a string or Tensor. (Can not convert a int into a Tensor or Operation.)

    6月5日的時候,修改dilated_seg.py(使用tensorflow)出現了報錯: TypeError: Fetch argument 0 has invalid type <type ' ...

  9. Dart常见类型转换 Int String Double

    int -> string age.toString() string -> int int.parse('100'); String -> double var onePointO ...

随机推荐

  1. webstorm拉取git代码

    在webstorm中VCS → git → clone → url就是你的git代码地址,parent Directory(你要放到的目录),Directiory Name(起一个项目名称)

  2. transform对定位元素的影响

    1.温故知新 absolute:生成绝对定位的元素,相对于除position:static 定位以外的第一个有定位属性的父元素进行定位,若父元素没有定位属性则相对于浏览器窗口的左上角定位,定位的元素不 ...

  3. java使用poi实现excel表格生成

    通过使用poi技术生成Excel,使用反射技术实现自动映射列表的数据. ExportTableUtil.java public class ExportTableUtil { /** * * @Des ...

  4. Eclipse-环境搭建(缅怀篇)

    JDK 下载jdk安装并配置环境变量运行java -version查看是否安装配置成功 Eclipse 下载eclipse,直接解压到目录 eclipse配置jre 设置complie编译等级 Ecl ...

  5. 《深入理解mybatis原理3》 Mybatis数据源与连接池

    <深入理解mybatis原理> Mybatis数据源与连接池 对于ORM框架而言,数据源的组织是一个非常重要的一部分,这直接影响到框架的性能问题.本文将通过对MyBatis框架的数据源结构 ...

  6. 三十一 Python分布式爬虫打造搜索引擎Scrapy精讲—chrome谷歌浏览器无界面运行、scrapy-splash、splinter

    1.chrome谷歌浏览器无界面运行 chrome谷歌浏览器无界面运行,主要运行在Linux系统,windows系统下不支持 chrome谷歌浏览器无界面运行需要一个模块,pyvirtualdispl ...

  7. Valgrind查找内存泄露利器

    Valgrind是一个GPL的软件,用于Linux(For x86, amd64 and ppc32)程序的内存调试和代码剖析.你可以在它的环境中运行你的程序来监视内存的使用情况,比如C 语言中的ma ...

  8. 4666 Hyperspace stl

    当时自己做的时候没有这么想,想的是每个象限去找一个无穷值来作为比较点.但是很麻烦 代码: #include <stdio.h> #include <string.h> #inc ...

  9. djagno重写authenticate实现帐号和邮箱的多方式登录。

    1.在users应用的view中重写authenticate: from django.contrib.auth.backends import ModelBackend from django.db ...

  10. 【前端安全】 web前端安全编码(模版篇)【转】

    在web的开发的开发过程中,前端总是在处理后端打的各种变量,变量可以包含着中的各种各样的字符,如果不对这些字符进行”特殊“处理的话,轻者导 致页面不正常的显示,潜入了其他的东西,亦即页面挂了,或者弹出 ...