Xcode CGFloat to NSInteger conversion

HYEONG HWAN, MUN/ 10월 23, 2014/ 미분류/ 0 comments

https://blog.lael.be/post/379

w3

There are two ways to Convert From CGFloat to NSInteger.

Solution one.
Convert CGFloat to NSString, then take integer value.

CGFloat foo = 540.0f;
NSInteger bar = [[NSString stringWithFormat:@"%.2f",foo] integerValue];

Solution two.
Just take Integer value.

CGFloat foo = 540.0f;
NSInteger bar = (int)foo;

Additional useful Infomartion

CGFloat foo = 540.4f;
CGFloat bar = ceil(foo);//541.0f  round-up
CGFloat bar2 = floor(foo);//540.0f  round-down
CGFloat bar3 = round(foo);//540.0f  round

 

Leave a Comment

작성하신 댓글은 관리자의 수동 승인 후 게시됩니다.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>
*
*