TIP: Round a Decimal to an Integer

How will your code round decimals to integers? Most people will focus the decimal fraction on the round off topic, so that they will separate the integer and the decimal fraction at first, and round the integer if the decimal fraction is equal to or lager than 0.5. So, here is a common code:

// Original round off function.
template <class T> int Round(T &value)
{
   int nInt = (int) value;
   T tDecimal = value - nInt;
   if( 0 <= value)
   {
      if( tDecimal >= 0.5 )
         nInt += 1;
   }
   else
   {
   if( tDecimal <= -0.5 )
         nInt -= 1;
   }

   return nInt;
}

However, you don't need to determine so many things. The system will copy only the integer number if you cast the value to be the type of the integer. See the disassembly code of int nInt = (int) value, as shown below:

0041206E  mov         eax,dword ptr [value]
00412071  fld         qword ptr [eax]
00412073  call        @ILT+220(__ftol2_sse) (4110E1h)
00412078  mov         dword ptr [nInt],eax

So, here is my solution:

// New round off function.
template <class T> int Round(T &value)
{
   if( 0 <= value)
      return (int) (value + 0.5);
   else
      return (int) (value - 0.5);
}

The value will round off after adding 0.5 if its decimal fraction is larger than 0.5.

Furthermore, this solution also will save operations, stacks, so as time, that is, the new round off solution is simpler and more efficient. You can see both round off functions in the disassembly to compare. You will feel the benefit is obvious, especially when running a game engine.

Let me test by using the following code:

int _tmain(int argc, _TCHAR* argv[])
{
   double d = 0;
   int n = 0;

   d = 123.55;
   n = Round(d);
   printf("Round(%f) is %d\n", d, n);

   d = 123.05;
   n = Round(d);
   printf("Round(%f) is %d\n", d, n);

   d = -123.55;
   n = Round(d);
   printf("Round(%f) is %d\n", d, n);

   d = -123.05;
   n = Round(d);
   printf("Round(%f) is %d\n", d, n);

   return 0;
}

and its output is:

Round(123.550000)  is  124
Round(123.050000)  is  123
Round(-123.550000) is -124
Round(-123.050000) is -123


About the Author

Reta Pan

A R&D Software Engineer in Taiwan.
International High IQ Society Member, New York.
MCSD(Microsoft Certified Solution Developer (for Microsoft .NET))
Web Site: Anpino's Circle
English is not my primary language, I will be glad to receive your response, especially the advises that you can provide regarding my poor English.

Comments

  • I always thought

    Posted by treuss on 10/21/2008 08:44am

    the solution presented in this article was the obvious choice. Hmmm.

    Reply
  • Common Rounding

    Posted by hoxsiew on 07/09/2008 03:01pm

    This is "Common Rounding" and will introduce a bias due to more rounding up than down. See: http://en.wikipedia.org/wiki/Rounding

    • Round-to-even method

      Posted by Anpino on 07/13/2008 11:44pm

      Or,
      return (int)(value + 0.4 + (0.1 * ((int)((value + 0.5)/2) - (int)(value/2))));

      Reply
    • Round-to-even method

      Posted by Anpino on 07/11/2008 05:06am

      Hi hoxsiew, thank you for your useful reference.
      Here is my current Round-to-even method for only positive number, 
      I will explain it, or give another better solution if I comprehend. 
      You can see when this article be updated.
      
      return (int)(value + 0.4 + (0.1 * (int)(value - 2 * (int)(value/2))));

      Reply
    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date