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
2009-06-28 20:21:59
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
2010-04-25 14:22:07
@Gavin Brock so then this answer is wrong.
Yar
2010-07-09 15:16:23
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
2010-07-23 19:36:57
Thanks Joe - I've updated the solution.
Ben Gotow
2010-07-25 05:05:21