Rounding Function

Page 1 of 1

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

#1 jjl

• Engineer

Reputation: 1270
• 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

Reputation: 454
• 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.

#3 jjl

• Engineer

Reputation: 1270
• 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.