Python round up to the nearest 10

Python – Round Number to Nearest 10

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.

In this tutorial, we will write Python programs to round a number to its nearest 10 or 100.

For example, if the number is 3652, then its nearest number to 10 is 3650. And the nearest 100 is 3700.

Example 1 – Round Number to Nearest 10 using round[]

In this example, we will read a number from user, and round the value to its nearest 10 using round[] function.

Python Program

number = int[input['Enter a number :']]
rounded = round[number/10]*10
print['Rounded Number :', rounded]

Output

Enter a number :3652
Rounded Number : 3650

Or you can also provide a negative number as the second argument for round[] function, to round to those number of digits before decimal point.

Python Program

number = int[input['Enter a number :']]
rounded = round[number, -1]
print['Rounded Number :', rounded]

Output

Enter a number :3652
Rounded Number : 3650

Example 2 – Round Number to Nearest 100 using round[]

In this example, we will round the number to its nearest 100 using round[] function.

Python Program

number = int[input['Enter a number :']]
rounded = round[number/100]*100
print['Rounded Number :', rounded]

Output

Enter a number :3652
Rounded Number : 3700

Or you can also provide a negative number as the second argument for round[] function, to round to those number of digits before decimal point. To round to nearest 100, we need to provide -2 as second argument to round[].

Python Program

number = int[input['Enter a number :']]
rounded = round[number, -2]
print['Rounded Number :', rounded]

Output

Enter a number :3652
Rounded Number : 3700

Conclusion

Concluding this Python Tutorial, we learned how to use round[] function to round a number to nearest 10 or 100. You can use the same process to find the nearest number to any digit before the decimal point.

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.

asked Oct 19, 2014 at 19:47

3

round does take negative ndigits parameter!

>>> round[46,-1]
50

may solve your case.

answered Oct 19, 2014 at 19:51

5

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

answered Oct 19, 2014 at 19:58

ParkerParker

8,32910 gold badges68 silver badges95 bronze badges

5

Here is one way to do it:

>>> n = 46
>>> [n + 9] // 10 * 10
50

answered Oct 19, 2014 at 19:50

NPENPE

471k103 gold badges921 silver badges996 bronze badges

3

This will round down correctly as well:

>>> n = 46
>>> rem = n % 10
>>> if rem < 5:
...     n = int[n / 10] * 10
... else:
...     n = int[[n + 10] / 10] * 10
...
>>> 50

answered Oct 19, 2014 at 20:02

2

How do you round up to the nearest 10?

Key Facts and Summary.
Rounding off to the nearest 10 means that we wish to round off the last two digits of a number, i.e. up to the tens place of a number..
If that digit is 0, 1, 2, 3, or 4, you will round down to the previous tens. [ Round down].
If that digit is 5, 6, 7, 8, or 9, you will round up to the next ten. [.

How do I get Python to round up?

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.

How do you round to the nearest 50 in Python?

Method 1: Using Built-in round[] Function. In Python there is a built-in round[] function which rounds off a number to the given number of digits. The function round[] accepts two numeric arguments, n and n digits and then returns the number n after rounding it to n digits.

How do you round to the nearest multiple of a number in Python?

Introduction to the Python round[] function The round[] function rounds the number to the closest multiple of 10-ndigits. In other words, the round[] function returns the number rounded to ndigits precision after the decimal point. If ndigits is omitted or None , the round[] will return the nearest integer.

Chủ Đề