# stupid question about converting floats to long ints...

• 08-05-2002
Brian
stupid question about converting floats to long ints...
Code:

`  loopspersec = lps / spd;`
because I'm trying to assign a Long Int from a Float.

My question:
Is there any function to convert a float to an integer?

Code:

```#include <stdio.h> #include <time.h> #include <stdlib.h> #include <limits.h> #include <math.h> #define TOTAL_LOOPS 500000000 int main (void) {   clock_t s, e;   float spd, dur;   long loopspersec;   long lps;     if( TOTAL_LOOPS > LONG_MAX)   {   printf("Hmmm, this shouldn't happen.");   return -1;   }     s = clock();    for (lps = 0; lps < TOTAL_LOOPS; lps++ );   e = clock();     dur = e - s;   spd = dur / CLOCKS_PER_SEC;   loopspersec = lps / spd;     printf ( "%ld Loops in %f seconds.\n", lps, spd );   printf ( "%ld Loops per second.\n", loopspersec);   system("PAUSE");   return 0; }```
• 08-05-2002
Prelude
The warning is about a loss of precision when you convert a float to a long. To remove this warning, you can cast the result of the division to long.

>loopspersec = lps / spd;
loopspersec = (long)(lps / spd);

Though a better method would be to use float for loopspersec. That way you can simply ignore everything past the decimal and you have the added advantage of being able to hold larger values. :)

-Prelude
• 08-05-2002
Brian
Just what I needed, thanks :)