IOS Number 处理(int-->NSNumber,NSNumber-->nsinteger,string -->double,CGFloat --> dobule)
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语言的基本数据类型长度
- NSLog(@"The size of an int is: %lu bytes.",sizeof(int));
- NSLog(@"The size of a short int is: %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)); // Do any additional setup after loading the view,
结果:
- 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、格式化输出数据
- //整型
- int integerType = 5;
- //浮点型
- float floatType = 3.1415;
- //双浮点型
- double doubleType = 2.2033;
- //短整型
- short int shortType = 200;
- //长整型
- long long int longlongType = 7758123456767L;
- //c语言字符串
- char * cstring = "this is a string!";
- //整型
- NSLog(@"The value of integerType = %d",integerType);
- //浮点型
- NSLog(@"The value of floatType = %.2f",floatType);
- //双浮点型
- NSLog(@"The value of doubleType = %e",doubleType);
- //短整型
- NSLog(@"The value of shortType = %hi",shortType);
- //长整型
- NSLog(@"The value of longlongType = %lli",longlongType);
- //c语言字符串
- NSLog(@"The value of cstring = %s",cstring);
结果:
- 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.当需要使用int类型的变量的时候,可以像写C的程序一样,用int,也可以用NSInteger,但更推荐使用NSInteger,因为这样就不用考虑设备是32位的还是64位的。
2.NSUInteger是无符号的,即没有负数,NSInteger是有符号的。
3.有人说既然都有了NSInteger等这些基础类型了为什么还要有NSNumber?它们的功能当然是不同的。
NSInteger是基础类型,但是NSNumber是一个类。如果想要在NSMutableArray里存储一个数值,直接用NSInteger是不行的,比如在一个NSMutableArray里面这样用:
- NSMutableArray *array = [[NSMutableArray alloc]init];
- [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;
例子:
- NSNumber *num = [NSNumber numberWithInt:88];
- NSInteger integer = [num intValue];
5、NSString与NSInteger的相互转换
- NSInteger integerNumber = 888;
- NSString * string = [NSString stringWithFormat:@"%d",integerNumber];
- NSLog(@"string is %@", string);
- integer = [string intValue];
- 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)的更多相关文章
- iOS开发之int,NSInteger,NSUInteger,NSNumber的使用
1.首先先了解下NSNumber类型: 苹果官方文档地址:https://developer.apple.com/library/ios/documentation/Cocoa/Reference/F ...
- [BS-02] iOS数组、字典、NSNumber 新写法—— @[]、@{}
IOS数组.字典.NSNumber 新写法—— @[].@{} //标准写法 NSNumber * number = [NSNumber numberWithInt:]; NSArray * ar ...
- NSNumber与NSInteger的区别 -bei
基本类型,如同C 语言中的 int 类型一样,拿来就可以直接用. 而类在使用时,必须先创建一个对象,再为对象分配空间,接着做初始化和赋值. 类的初始化,需用类自身的方法 (类方法). 代码中所创建的对 ...
- NSNumber与NSInteger的区别
Objective-C 支持的类型有两种:基本类型 和 类. 基本类型,如同C 语言中的 int 类型一样,拿来就可以直接用. 而类在使用时,必须先创建一个对象,再为对象分配空间,接着做初始化和赋值 ...
- iOS学习14之OC NSNumber + NSValue
1.NSNumber 数值类. 作用:实现基本数据类型与OC对象类型的相互转化. 1> NSNumber创建对象 // 初始化方法 NSNumber *num1 = [[NSNumber all ...
- 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 ...
- 【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); ///////////// ...
- 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 ' ...
- Dart常见类型转换 Int String Double
int -> string age.toString() string -> int int.parse('100'); String -> double var onePointO ...
随机推荐
- QT 样式表实例
目标:实现button的圆角效果及背景颜色,鼠标滑过颜色变亮,鼠标点击颜色变重. 总体思路首,先根据需要及样式规则新建.qss文件,然后在代码中将文件引用并应用样式. 具体过程如下: 1在项目当前目录 ...
- 拉取代码过程中遇到的:post install error,please remove node_modules before retry!
这是在git → clone 之后,安装npm intall时出现的错误,完整错误提示如下: 解决: // 1.先删除node_modules这个文件 $ rm -rf node_modules/ / ...
- 嵌入式 Web workers
前言 虽然worker可以将复杂的运算放入单独线程去运算,不阻塞UI线程,但是,由于worker()的构造函数的参数不能读取本地的文件,只能来自网络,所以当在一个项目里想使用本地的模块函数,是一个很麻 ...
- CSS之position体验
目录: 1. position介绍 2. relative 3. position 4. fixed与static 5. 总结 1. position介绍 position最简单的理解就是元素位置的定 ...
- python学习笔记(字典乱码)
博主总结下 python中字典中包含中文时,使用过程中出现乱码 json.dumps(params, encoding="UTF-8", ensure_ascii=False) p ...
- springmvc的@Validated/@Valid注解使用和BindingResult bindingResult
关于@Valid和Validated的比较 @Valid是使用hibernate validation的时候使用 @Validated 是只用spring Validator 校验机制使用 一:@V ...
- HDU - 5988The 2016 ACM-ICPC Asia Qingdao Regional ContestG - Coding Contest 最小费用流
很巧妙的建边方式 题意:有n个区域,每个区域有一些人数si和食物bi,区域之间有m条定向路径,每条路径有人数通过上限ci.路径之间铺了电线,每当有人通过路径时有pi的概率会触碰到电线,但是第一个通过的 ...
- 1004: [HNOI2008]Cards burnside定理
https://www.lydsy.com/JudgeOnline/problem.php?id=1004 输入数据保证任意多次洗牌都可用这 m种洗牌法中的一种代替,且对每种洗牌法,都存在一种洗牌法使 ...
- 《深入理解mybatis原理4》 MyBatis缓存机制的设计与实现
<深入理解mybatis原理> MyBatis缓存机制的设计与实现 本文主要讲解MyBatis非常棒的缓存机制的设计原理,给读者们介绍一下MyBatis的缓存机制的轮廓,然后会分别针对缓存 ...
- .pth 文件扩展python环境路径
有时候我们不希望把一个库放到 site-packages 下面,而是更愿意把它保留在原始的工程目录中,方便管理和维护. 通常的做法是在程序启动的时候,往sys.path里面增加这个目录,但是这样做非常 ...