Round integers to the nearest 10

后端 未结 7 1651
孤街浪徒
孤街浪徒 2020-11-27 17:45

I am trying to round integers in python. I looked at the built-in round() function but it seems that that rounds floats.

My goal is to round integers to the closest

相关标签:
7条回答
  • 2020-11-27 18:35

    I wanted to do the same thing, but with 5 instead of 10, and came up with a simple function. Hope it's useful:

    def roundToFive(num):
        remaining = num % 5
        if remaining in range(0, 3):
            return num - remaining
        return num + (5 - remaining)
    
    0 讨论(0)
提交回复
热议问题