Round integers to the nearest 10

后端 未结 7 1658
孤街浪徒
孤街浪徒 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:08

    round() can take ints and negative numbers for places, which round to the left of the decimal. The return value is still a float, but a simple cast fixes that:

    >>> int(round(5678,-1))
    5680
    >>> int(round(5678,-2))
    5700
    >>> int(round(5678,-3))
    6000
    

提交回复
热议问题