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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sjmiller05
    New Member
    • Nov 2006
    • 1

    #1

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

    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!
  • manontheedge
    New Member
    • Oct 2006
    • 175

    #2
    you can use IF statements...

    if ( number <= 1499 )
    number = 1000;

    if ( number > 1499 )
    number = 2000;

    ...something like that

    Comment

    • horace1
      Recognized Expert Top Contributor
      • Nov 2006
      • 1510

      #3
      a possible algorithm is
      (1) number is an int value 5678
      (2) mulltiplier is an int initial value 10
      (3) have a loop
      Code:
          while(number > multiplier*10) multiplier *= 10;
      (4) evaluating
      Code:
      (int)(ceil( (float) number / multiplier))  * multiplier
      gives 6000

      Comment

      Working...