2

I have a this small task , where I am rounding up the number to higher multiple of ten. But want to round it to nearest multiple of ten.

My code :

import math

a = map(int,list(str(7990442)))
b = map(int,list(str(1313131)))
print "a :",a
print "b :",b

l= []
for p,q in zip(a,b):
    l.append(p*q)
print "prod list :",l
ls = sum(l)
print "sum :",ls
def roundup(x):
    return int(math.ceil(x / 10.0)) * 10
top = roundup(ls)
print "round value: ",top
val = top-ls
print "val :",val
a.append(val)
print "output :",a

Output :

a : [7, 9, 9, 0, 4, 4, 2]
b : [1, 3, 1, 3, 1, 3, 1]
prod list : [7, 27, 9, 0, 4, 12, 2]
sum : 61
round value:  70
val : 9
output : [7, 9, 9, 0, 4, 4, 2, 9]

Expected Output :

sum : 61
round value:  60
val : 1
output : [7, 9, 9, 0, 4, 4, 2, 1]
Shivkumar kondi
  • 6,458
  • 9
  • 31
  • 58
  • How should edge cases like 65 or 75 be handled? Do you want them consistently rounded up, consistently rounded down or some other variation? – Tagc Jan 25 '17 at 13:53
  • @Tagc Yes If number = 65 it should be rounded up to 70 – Shivkumar kondi Jan 25 '17 at 13:55
  • Do you want all numbers midway between two multiples of ten to be consistently rounded up then? e.g. 65 -> 70, 75 -> 80? – Tagc Jan 25 '17 at 13:56
  • Yes for all middle number(5) they must be rounded up and even I need to adjust the val number to be positive only, In my case it should in range(0-9) – Shivkumar kondi Jan 25 '17 at 13:56

3 Answers3

6

You can use round(x / 10.0) * 10 instead of math.ceil.

Even easier would be

def custom_round(x):
    return int(round(x, -1))

This immediately rounds to a multiple of ten.

instant
  • 676
  • 6
  • 12
  • 2
    Note that because of the way `round` works, this implementation will produce some possibly unexpected results: `custom_round(65) = 60`, `custom_round(75) = 80`. Not sure if this matters to OP. – Tagc Jan 25 '17 at 13:52
  • 1
    That is actually a very good point, I've been 'burned' by IEEE rounding rules before. For those who don't know: the rule is to round to the nearest _even_ number: https://en.wikipedia.org/wiki/IEEE_floating_point#Rounding_rules This is meant to prevent statistical bias in large samples. – instant Feb 12 '17 at 22:01
3

For the general purpose, given a base to round to:

def my_special_round(x, base=10):
    return int(base * round(float(x)/base))
Gal Dreiman
  • 3,969
  • 2
  • 21
  • 40
1

To find the smallest multiple of ten, I would do something like this. This way I didn't need to use Math Lib:

while True:
    n=int(input("number"))
    n=n/10
    n=int(n) # it removes decimal
    n=n*10 #then by make it times 10 to return to proportion
    print(n)

In this code I used the int casting to get rid of the unit.

If you want to find the nearest (including the top one) you could use an if unit is more than 5, then add one:

while True:
    n=int(input("number"))
    if (n%10>5):
        n=n+10
    n=n/10
    n=int(n)
    n=n*10
    print(n)
brian d foy
  • 129,424
  • 31
  • 207
  • 592
Breno Baiardi
  • 99
  • 1
  • 16