-1

Is there any method or easy way to round up integers to desired point(hundreds, thousands etc)? Yea, I know can use math lib, can use functions and etc, but seems very complicated for simple thing. At least old python version I couldnt find out something good.

13 -> 20;
2237 -> 2240;
3451 -> 3500

My situation: dict = {'Key':[2312, 467]}

Need to get: dict = {'Key':[2320, 470]}

Show you examples, also show when need to round down, or round in math way(0-4 to 0, 5-9 to 10)

  • Does this answer your question? [Round integers to the nearest 10](https://stackoverflow.com/questions/3348825/round-integers-to-the-nearest-10) – Rafael-WO Sep 22 '21 at 06:34
  • Is there any logic behind what rounds to the nearest hundred, and what rounds to the nearest ten? – ddejohn Sep 22 '21 at 06:40
  • Why is 13 --> 20 and not 10? – Karina Sep 22 '21 at 07:18
  • @Karina, dont understand question. Because have task where can be only bigger value rounded to 10. – Justme4ever Sep 22 '21 at 09:32
  • @ddejohn I can have different numbers/values from tens to thousands and all must be rounded up to tens. Asked more widely than I need, just to have understanding how u r solving it. – Justme4ever Sep 22 '21 at 09:34
  • @Justme4ever my bad, misread that part. – Karina Sep 22 '21 at 09:41
  • @Justme4ever if they're rounded to the nearest ten, then `3451` should round to `3460`. – ddejohn Sep 22 '21 at 14:18
  • Does this answer your question? [Python round up integer to next hundred](https://stackoverflow.com/questions/8866046/python-round-up-integer-to-next-hundred). It's about rounding up to the next hundred rather than the next 10, but it's the same in principle. tl;dr: just map each number `x` to `x + (-x) % 10`. – Mark Dickinson Sep 22 '21 at 15:17
  • @MarkDickinson Ya, pretty solid. Overlooked. Ty. – Justme4ever Sep 22 '21 at 18:48

3 Answers3

2

Ceil and floor equivalent in Python 3 without Math module?

This should do like what you want.

round_degree = 100
number = 2151


# Example rounding down to nearest 100
rounded_down = (number // round_degree) * round_degree
print(number, rounded_down )


# Example rounding up to nearest 100
rounded_up = -(-number // round_degree) * round_degree
print(number, rounded_up)
ddejohn
  • 8,775
  • 3
  • 17
  • 30
zasc
  • 44
  • 4
0

You could do it like this:-

D = {'Key1': [2312, 467], 'Key2': [123, 129]}

for k, v in D.items():
    D[k] = [int(round(_n, -1)) for _n in v]

print(D)
  • Before I have dict Im putting it in and I can slice to get last value and compared to 0, if lastNumber not 0 10-lastNumber + numberValue. So how to solve I know, but I am looking for elegant short option :). – Justme4ever Sep 22 '21 at 09:41
  • 1
    I must apologise for presenting inelegant code. I promise not to do it again –  Sep 22 '21 at 10:21
  • 1
    Just a heads up, `dict` is the name of the built-in data type in Python. By using it as a variable name you're overwriting that type's constructor which is a bad idea. – ddejohn Sep 22 '21 at 14:22
  • Quite right @ddejohn - I took it from the original question without thinking. I've edited it now –  Sep 22 '21 at 15:17
  • @DarkKnight lol I can't stop laughing – Karina Sep 22 '21 at 15:37
0

You can implement your version of round

def round(number, digits):
    mult = 10**(digits)
    val = int((number*mult)+0.5*(1 if number > 0 else -1))/mult 
    return val

for k, v in dict.items():
   dict[k] = [ round(number, -1) for number in v ]