views:

3236

answers:

1

Any idea on how to convert int to CGFloat in objective C?

+3  A: 

Hey Shoaibi,

In the past casting a CGFloat value using the () syntax has worked fine for me. CGFloat is just defined as "typedef float CGFloat;" so you go about casting it the same way you would a float:

CGFloat f = (CGFloat)intVal;

or, if your value is a constant:

CGFloat f = 1.10;
Ben Gotow
Sadly this no longer works on 64bit, where a CGFloat is a 'double': http://stackoverflow.com/questions/1264924/whats-the-difference-between-using-cgfloat-and-float
Gavin Brock
@Gavin Brock so then this answer is wrong.
Yar
If you remove the `f` suffix then it should work, there will be an implicit downcast from double to float on a 32-bit platform and no cast at all on a 64-bit one.
Joe D
Thanks Joe - I've updated the solution.
Ben Gotow