2 Replies - 1765 Views - Last Post: 04 March 2012 - 02:30 PM

#1 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1270
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Rounding Function

Posted 20 October 2009 - 12:06 PM

Description: Input any double into the function and it will return with the proper rounded integerRounds positive and negitive numbers correctly
int round(double number)
{
	double x = .5;
	if(number<0)
		x = -.5;
	return static_cast<int>(number+x);
}


Is This A Good Question/Topic? 0
  • +

Replies To: Rounding Function

#2 Karel-Lodewijk   User is offline

  • D.I.C Addict
  • member icon

Reputation: 454
  • View blog
  • Posts: 864
  • Joined: 17-March 11

Re: Rounding Function

Posted 11 February 2012 - 02:46 AM

Most common and more efficient implementation of round to nearest is. int number = x + .5; Think about it adding .5 and rounding down will always round to the nearest integer.
Was This Post Helpful? 0
  • +
  • -

#3 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1270
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: Rounding Function

Posted 04 March 2012 - 02:30 PM

Yeah you are correct, this was written 3 years ago when I first started programming.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1