tags:

views:

970

answers:

10

Dynamic integer will be any number from 0 to 150.

i.e. - number returns 41, need to return 50. If number is 10 need to return 10. Number is 1 need to return 10.

Was thinking I could use the ceiling function if I modify the integer as a decimal...? then use ceiling function, and put back to decimal?
Only thing is would also have to know if the number is 1, 2 or 3 digits (i.e. - 7 vs 94 vs 136)

Is there a better way to achieve this?

Thank You,

+31  A: 
n + (10 - n % 10)

How this works. The % operator evaluates to the remainder of the division (so 41 % 10 evaluates to 1, while 45 % 10 evaluates to 5). Subtracting that from 10 evaluates to how much how much you need to reach the next multiple.

The only issue is that this will turn 40 into 50. If you don't want that, you would need to add a check to make sure it's not already a multiple of 10.

if (n % 10)
    n = n + (10 - n % 10);
R Samuel Klatchko
Ah, this is faster than mine.
Harvey
I'd normally go for an integer math approach (which works due to the implicit rounding because of the types of the arguments). I like this approach as it has no implicit effects and thus is less likely to fail.
Epsilon Prime
He already covered that case in his answer, Matthew.
Marc W
Well, it's not faster anymore. :) Actually, mine's faster now having one add, one mult, one divide, and no branches.
Harvey
You can factor out the mod, so it's not performed a second time.
Steven Sudit
@Harvey: It's wise to not make performance claims until you've measured it. The compiler is probably smarter than all of us.
Greg Hewgill
Why not `(n+9) - ((n+9)%10)` ? This works for multiples of 10 too, and still requires only three operations.
Mark Dickinson
Does this work for negative values? The result of % is negative in that case. -5 + (10 - -5 % 10) = -5 + 10 - -5 = 10 != 0.
Strilanc
Why not: n+ ((10-n)%10)?
Stefan Kendall
@Greg: True. Lines of assembly from gcc -S: R(40), Mine(19), Mark(21). @Mark: I think I like your solution the best: n+=9; n-=n%10;
Harvey
why not --n + (10 - n % 10)
KevinDTimm
There's a much simpler solution down the page.n = (n+9)/10.
Stefan Kendall
@Strilanc: Whether this works for negatives depends on what you need, what you consider to be the right answer. Could be either way.
AndreyT
OP didn't want negatives ....
KevinDTimm
A: 

In pseudo code:

number = number / 10
number = ceil(number)
number = number * 10

In Python:

import math
def my_func(x):
    return math.ceil(x / 10) * 10

That should do it. Keep in mind that the above code will cast an integer to a float/double for the arithmetic, and it can be changed back to an integer for the final return. Here's an example with explicit typecasting

In Python (with typecasting):

import math
def my_func(x):
    return int(math.ceil(float(x) / 10) * 10)
Mike Trpcic
Only if number is floating-point, or am I missing something?
Space_C0wb0y
The number can be converted to floating point for the arithmetic, and casted back to an integer during the return.
Mike Trpcic
For Python, a much nicer solution is simply `n + -n % 10`. This won't work in C though, because of the different semantics for divisions involving negative numbers.
Mark Dickinson
It is far simpler to take advantage of the natural machine level integer arithmetic.
Clifford
+4  A: 

How about using integer math:

N=41
N+=9   // Add 9 first to ensure rounding.
N/=10  // Drops the ones place
N*=10  // Puts the ones place back with a zero
Harvey
Doesn't match the OP's requirements.
Matthew Flaschen
that will compute the floor, to get the ceiling first add 9
cobbal
@cobbal: thanks, I missed that.
Harvey
+2  A: 

You could do the number mod 10. Then take that result subtract it from ten. Then add that result to the original.

if N%10 != 0  #added to account for multiples of ten 
  a=N%10
  N+=10-a
Alex
Doesn't work for negative numbers.
Jive Dadson
Same mistake. Turns 10 into 20.
AndreyT
@Jive Dadson: The input requirement was an integer from 0 to 150, so -ve is not an issue in this case.
Clifford
+21  A: 

You can do this by performing integer division by 10 rounding up, and then multiplying the result by 10.

To divide A by B rounding up, add B - 1 to A and then divide it by B using "ordinary" integer division

Q = (A + B - 1) / B 

So, for your specific problem the while thing together will look as follows

A = (A + 9) / 10 * 10

This will "snap" A to the next greater multiple of 10.

The need for the division and for the alignment comes up so often that normally in my programs I'd have macros for dividing [unsigned] integers with rounding up

#define UDIV_UP(a, b) (((a) + (b) - 1) / (b))

and for aligning an integer to the next boundary

#define ALIGN_UP(a, b) (UDIV_UP(a, b) * (b))

which would make the above look as

A = ALIGN_UP(A, 10);

P.S. I don't know whether you need this extended to negative numbers. If you do, care should be taken to do it properly, depending on what you need as the result.

AndreyT
+1 This is the only integer-arithmetic solution so far that actually works when n=10 without an extra conditional test.
Greg Hewgill
Well, this is actually a rather well-known and widely used idiom. I'm surprized I was the first to mention it.
AndreyT
Me too. I'm surprised at the number of answers that were actually *wrong* to start with (the n=10 case was specifically mentioned in the question, too).
Greg Hewgill
+3  A: 

in C, one-liner:

int inline roundup10(int n) {
  return ((n - 1) / 10 + 1) * 10;
}
Utaal
sorry sorry, now it works :)
Utaal
Doesn't work for negative numbers.
Jive Dadson
@Jive Dadson: Negative numbers are outside of the domain of the OP's question
bta
+1  A: 

Be aware that answers based on the div and mod operators ("/" and "%") will not work for negative numbers without an if-test, because C and C++ implement those operators incorrectly for negative numbers. (-3 mod 5) is 2, but C and C++ calculate (-3 % 5) as -3.

You can define your own div and mod functions. For example,

int mod(int x, int y) {
  // Assert y > 0
  int ret = x % y;
  if(ret < 0) {
    ret += y;
  }
  return ret;
}
Jive Dadson
OP specified that this function will only operate on numbers 0-150.
bta
+12  A: 

What about ((n + 9) / 10) * 10 ?

Yields 0 => 0, 1 => 10, 8 => 10, 29 => 30, 30 => 30, 31 => 40

bta
This seems to be the simplest solution.
Stefan Kendall
Er... `(n + 9) / 10` yields `8 => 1`, not `8 => 10` as you state in your post. You probably forgot to multiply the result back by `10`.
AndreyT
@ AndreyT: >.< thanks for catching my typo.
bta
+1: Good answer. I have used it recently in a bitset implementation. Q: If a word can hold 32 bits, how many words are required to hold 'n' bits (n>=0)? A: (n+31)/32
ArunSaha
A: 
n + (((9 - (n % 10)) + 1) % 10)
Ignacio Vazquez-Abrams
A: 
round_up(int i) 
{
      while(i%10) {
            i++;
      }
      return(i);
}
Bram