Rounding

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2008
Posts: 3
Reputation: brent.lozano001 is an unknown quantity at this point 
Solved Threads: 0
brent.lozano001 brent.lozano001 is offline Offline
Newbie Poster

Re: C++ if statement help

 
0
  #1
Sep 16th, 2008
im trying to learn how to make a if/then statement that will take a number, possibly with a decimal and round it to the nearest integer. Like so..

if 3.4 is the number, then the final would be 3 not 3.4

how would i do so?
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,497
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 123
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: Rounding

 
0
  #2
Sep 16th, 2008
Rounding a number to the nearest whole number is one of the easiest things you can do in C++ there isn't even any need for an if/else statement, take this as an example.
  1. float decimal = 3.4f;
  2. int rounded = int(decimal + 0.5f); // 3
Hope this helps.
Last edited by William Hemsworth; Sep 16th, 2008 at 5:40 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 2
Reputation: erabyss is an unknown quantity at this point 
Solved Threads: 0
erabyss erabyss is offline Offline
Newbie Poster

Re: Rounding

 
0
  #3
Sep 16th, 2008
Yes it does. Thank you.

I wasnt the one who posted the question, but I am workin on a program that has a sim problem. Its a calculater that that works out the ultimate return on a purchase. I am writing it for an MMORPG I play.

This was a big help, thnk you. =]
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Rounding

 
1
  #4
Sep 16th, 2008
1. Regrettably, for negative numbers williamhemsworth's solution does not work (moreover, gives wrong result - for example, it yields 0 for -0.9 but nearest integer value for -0.9 is -1). Right solution:
  1. #include <cmath>
  2. ...
  3. double round(double x) {
  4. bool negative = false;
  5. if (x < 0.0) {
  6. x = -x;
  7. negative = true;
  8. }
  9. x = std::floor(x+0.5);
  10. return negative? -x: x;
  11. }
2. In this context a rounding to the nearest integer is not the same thing as a rounding to the nearest int type value. For example, on 32-bit CPU max int is 2147483647 (INT_MAX macros from limit.h). So you can't round 3333333333.3 to int 3333333333 (no such int value) - but you can round it to 3333333333.0. Of course, if you want to round to int, you may use the same method as in round function defined above (no need in floor library call in that case).
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1029 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC