This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

math question (beginner)

I'm using float variables, but I want to convert these variables to integer


like: 0.9 .... 1
30.9 .... 31
54.2 .... 54


please give a suggestion

thanks very much

Parents
  • I was thinking of something like this but I see there is still one floating point math operation. Unless someone can think of a way to eliminate it, Drew's way might be best.

    int Convertor(float fSrc)
    {
       if ((fSrc - (int)fsrc) >= 0.5)
          return ((int)fSrc+1)); // instead of return((int)(fSrc+1));
    
       else
          return( (int)fSrc);
    }
    Walt

Reply
  • I was thinking of something like this but I see there is still one floating point math operation. Unless someone can think of a way to eliminate it, Drew's way might be best.

    int Convertor(float fSrc)
    {
       if ((fSrc - (int)fsrc) >= 0.5)
          return ((int)fSrc+1)); // instead of return((int)(fSrc+1));
    
       else
          return( (int)fSrc);
    }
    Walt

Children