1

I know that I can round to the closest multiple of 10 in python by using round(<int>, -1) Is there a way to do this without using this built in function?

Edit:

Thanks for the feedback! The answer using divmod() was interesting because I had never used divmod before. Just in case anyone wants to know on CodingBat the solution used modulo as was suggested in the comments. Here it is in case anyone is interested.

def round10(num):
  mod = num % 10
  num -= mod
  if mod >= 5: num += 10
  return num
3
  • 3
    This sounds like an interview question Commented Dec 26, 2013 at 18:33
  • It's not, it is from a practice puzzle on CodingBat.com I was just trying to do it without using the round() function. The puzzle is here codingbat.com/prob/p179960 Commented Dec 26, 2013 at 18:37
  • Could you use modulo? Commented Dec 26, 2013 at 18:42

4 Answers 4

2

divide by 10, int, multiply by 10.

Actually, you can do it without any builtins using the // operator:

>>> def round(x):
...     return (x//10)*10
... 
>>> round(15.)
10.0
>>> round(25.)
20.0

Of course, this always rounds down. If you want to round up for values with a remainder greater than 5, you could use divmod:

def round(x):
    n, remainder = divmod(x, 10)
    if remainder >= 5:
        n += 1
    return n * 10
Sign up to request clarification or add additional context in comments.

3 Comments

This won't round to the "closest multiple of 10", though (at least not how I'd interpret that phrase.)
This is one implementation of rounding (round down) but how would you deal with the more common (in the US at least) of round half up?
@JasonSperske -- Added that too.
2

This rounds to the closest, not always up or always down:

def xround(n):
    return (n + 5) // 10 * 10

1 Comment

This is clever. I've seen it used before -- Not sure why I didn't think of it. I would suggest integer division for python 3 compatability though (and for use with floats).
0

Round down to the closest multiple of 10:

int(n / 10) * 10

Round up to the closest multiple of 10:

int((n + 10) / 10) * 10

Round to the closest multiple of ten:

(int(n / 10) + bool(n % 10 >= 5)) * 10

1 Comment

I would appreciate it if whoever downvoted would give a reason for doing so, so that I can improve my answer.
0

I have a function that rounds n to the nearest multiple of d:

def cm(n,d,o="+"):
    p = d - (n % d)
    m = n % d
    nm = n - m
    np = n + p
    if p > m:
        return nm
    elif m > p:
        return np
    else:
        if o == "+":
            return np
        else:
            return nm

How to use: use cm(number, near multiple wanted, preferred direction in special cases)

Examples:

cm(8,10) = 10
cm(6,4,"-") = 4 #special case (n % d == d - n % d)
cm(6,4,"+") = 8 #special case
cm(6,4) = 8

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.