If I get the number 46 and I want to round up to the nearest ten. How do can I do this in python?
46 goes to 50.
Round() Round() is a built-in function available with python. It will return you a float number that will be rounded to the decimal places which are given as input. If the decimal places to be rounded are not specified, it is considered as 0, and it will round to the nearest integer.
You can use round(x / 10.0) * 10 instead of math. ceil . This immediately rounds to a multiple of ten.
To round number to nearest 10, use round() function. We can divide the value by 10, round the result to zero precision, and multiply with 10 again. Or you can pass a negative value for precision. The negative denotes that rounding happens to the left of the decimal point.
To implement the “rounding up” strategy in Python, we'll use the ceil() function from the math module. The ceil() function gets its name from the term “ceiling,” which is used in mathematics to describe the nearest integer that is greater than or equal to a given number.
round
does take negative ndigits
parameter!
>>> round(46,-1) 50
may solve your case.
You can use math.ceil()
to round up, and then multiply by 10
import math def roundup(x): return int(math.ceil(x / 10.0)) * 10
To use just do
>>roundup(45) 50
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With