Rounding is one of those things that seems to be a fairly difficult for programmers. Although the built-in rounding functions of many programming languages have helped a lot of programmers embarrass themselves, some still choose (or, in C/C++, are forced) to take a stab at it. But it's just a simple combination of floor(). ceil(), and arithmetic, you might think. An anonymous submitter ("Isostar") found out that some choose to use sprintf() for this ...

float Round(float val)
{
  TCHAR tmpStr[128];
  int intVal, decVal;
  int i;

  if(globalRounding)
  {
    _stprintf(tmpStr, _T("%0.1f"), val);
    intVal = _ttoi(tmpStr);

    for(i = 0; i < (int)_tcslen(tmpStr); i ++)
    {
      if(tmpStr[i] < '0' || tmpStr[i] > '9')
      {
        decVal = _ttoi(&(tmpStr[i + 1]));
      }
    }
    if(decVal <= 3)
    {
      val = (float)intVal;
    }
  }
  return val;
}

Of course, since that doesn't work out very well, the same developer was forced to create another function, RoundCorrect() ...

int RoundCorrect(float number)
{
    if(number-(int)number<0.01)
        return (int)number;
    else
        return (int)(number+1);
}

... which, in turn, led to yet another function, RoundCorrect2() ...

int RoundCorrect2(float number)
{
    if(number - (int)number < 0.5)
        return (int)number;
    else
        return (int)(number+1);
}
[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!