Python 3 code - Rounding integers

Question

I am trying to round numbers up in python 3. In my existing code, the number either round up to the nearest 10 or down. For example, 67 goes to 70 and 64 goes to 60. I would like the number to always round up to the nearest multiple of 10, so that 67-->70 and 64-->70. Here is my code for rounding so far:

##ROUNDING SumOfUsrinput TO NEAREST 10##
SumOfUsrinput=int(input("Please enter the sum: "))
SumRounded=round(SumOfUsrinput,-1)
print (SumRounded)

I would appreciate it if you could answer simple and explain how it works.


Show source
| python   | integer   | python-3.x   | rounding   2017-01-04 23:01 1 Answers

Answers ( 1 )

  1. 2017-01-05 00:01

    One way of rounding up would be to use integer division to go down to the precision you want and then multiplying back up. e.g.,:

    Sumrounded = SumOfusrinput // (-10) * (-10)
    
◀ Go back