C converting a float to an int
cmeehan
Posts: 8
Hi All,
I have written some C code using floats and I need to use a function that wants ints passed to it. How do convert a float to an int? I only care about the whole number part, if that matters.
Thanks
Chris
I have written some C code using floats and I need to use a function that wants ints passed to it. How do convert a float to an int? I only care about the whole number part, if that matters.
Thanks
Chris
Comments
But the general case gets a bit more complex than that. For a summary, check out http://www.cs.tut.fi/~jkorpela/round.html
Ross.
Thanks
Chris
PS - How do I mark a thread as "Solved"
int i;
float f;
f = (float) i;
I tried the above in C++/Simple IDE to convert an int to a double, and the double remained 0.00000 no matter what I did.
Hi cmeehan,
Edit your original post in advanced mode, and change the prefix box to "solved".
Regards,
DJ
While it seems to me that this should almost be done with a Rounding function, that may well not be enough to get C to see the number as an integer. This approach is actually a Rounding Down.
Going the other way might have been considered more hazardous as the computer depends heavily on integer to all sorts of representations of binary code. So the gnomes that made C++ might just have created an additional 'no-no' that requires some sort of override.
So far I have ignored C++ as it seems to add a lot of complexity.
For example, the following code should print i = 999, f = 999.000000 on any C or C++ compiler: