Back to writing
미분류 1 min read HYEONG HWAN, MUN

Xcode CGFloat to NSInteger conversion

w3

There are two ways to Convert From CGFloat to NSInteger.

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

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

Solution two.
Just take Integer value.

[code language="objc"]
CGFloat foo = 540.0f;
NSInteger bar = (int)foo;
[/code]

Additional useful Infomartion

[code language="objc"]
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
[/code]