开发者

Objective-C, how do I convert a double to an int?

I must be doing something really obviously wron开发者_高级运维g, but I can't see it.

Objective-C, how do I convert a double to an int?


A double is a C type, not an Objective-C object. Hence you use C casts:

double myDouble = 3.2;
int myInt = (int)myDouble;


Just converting mentioned above is good enough though you might want to use floor() or ceil() functions before that.


intValue is a method for a NSNumber instance. For scale type like int, double, and float, they are not class type. So, they have no methods. Some languages like C# may wrap int, or double as a object, and they can be transfered to each other by a sub-routine.


try this

NSInteger number=33;
NSUInteger count = (NSInteger)[number];

here, NSUInteger is long. number is NSInteger


double myDouble = 3.2;

int myInt = @(myDouble).intValue;

Sample of code I actually use:

NSNumber * percentLike1 = @(@(self.percentLike.doubleValue*100).integerValue);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜