views:

77

answers:

1

I have a CGFloat that contains only "integers", in the meaning that it actually wants to represent integers only but due to float inprecision it may happen that it internally has a 23.00000000000000000000000142 or something like that. I try to feed an NSDecimalNumber with clean input, so I need to make sure this is truly a naked integer with no dirt. Well, I think this is a good way, but maybe I am wrong:

NSInteger *intVal = floatVal;

That would just get rid of those fragmental parts at the end of the tale, right? Or is there a more secure way to convert it into a true integer?

+1  A: 

If you just want to take the integer part of a float then I believe you can just do the following:

CGFloat myfloat = 23.0000000000142f;
int myInt = (int) myfloat;
Tom
that makes sense. Would that also work with NSInteger? Would that make a difference than assigning the float to a NSInteger without prior casting it like this? I'm not sure what's going on under the hood but could imagine it does something like that?
HelloMoon
Yes it should work with NSInteger. If memory serves me correct NSInteger is defined as either an int or a long, either way this should still work.
Tom