By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,605 Members | 1,370 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,605 IT Pros & Developers. It's quick & easy.

"Rounding" in C++ --- HELP!!!

P: 1
I am writing a program for a C++ class and I need to be able to round my numbers up to the nearest 1000 (not 1000th, the actual number 1000). For example, if my variable value came back as 325, I would need a function to round it to 1000, if the variable came back as 1555, I would need to round to 2000. Any suggestions? Other than floor and ceiling functions, which do not appear to apply in my case, I cannot find any functions available in C++. I am very new to programming and I am not sure where to go from here.

Thanks!
Nov 12 '06 #1
Share this Question
Share on Google+
2 Replies


100+
P: 175
you can use IF statements...

if ( number <= 1499 )
number = 1000;

if ( number > 1499 )
number = 2000;

...something like that
Nov 12 '06 #2

Expert 100+
P: 1,510
a possible algorithm is
(1) number is an int value 5678
(2) mulltiplier is an int initial value 10
(3) have a loop
Expand|Select|Wrap|Line Numbers
  1.     while(number > multiplier*10) multiplier *= 10;  
  2.  
(4) evaluating
Expand|Select|Wrap|Line Numbers
  1. (int)(ceil( (float) number / multiplier))  * multiplier
  2.  
gives 6000
Nov 13 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.