To display a number rounded to the nearest hundred, with two digits after the decimal: print('{:0.2f}'.format(round(x, -2))) Tested: >>> x = 157395.85 >>> print('{:0.2f}'.format(round(x, -2))) 157400.00 Answer from totallygeek on reddit.com
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how to round a value in a print statement to the nearest 100?
r/learnpython on Reddit: How to round a value in a print statement to the nearest 100?
January 30, 2019 -

So I have a list of floats that I am manipulating in a function. On the output of the function, I would like to round this to the nearest 100. So I would like to rounding to be like this: 157395.85 ----> 157400.00.

Here is an example of the code I wrote:

x = 157395.85

print(round(x, 4))

This did not run correctly. Another issue I noticed was that if the float had a zero at the last digit after the decimal, the zero was not listed. What I mean is that 50000.50 is stored as 50000.5.

Any help would be great!

Edit: To all that have commented, your solutions were amazing thank you so much! I've upvoted all your comments.

Top answer
1 of 11
214

Rounding is typically done on floating point numbers, and here there are three basic functions you should know: round (rounds to the nearest integer), math.floor (always rounds down), and math.ceil (always rounds up).

You ask about integers and rounding up to hundreds, but we can still use math.ceil as long as your numbers smaller than 253. To use math.ceil, we just divide by 100 first, round up, and multiply with 100 afterwards:

Copy>>> import math
>>> def roundup(x):
...     return int(math.ceil(x / 100.0)) * 100
... 
>>> roundup(100)
100
>>> roundup(101)
200

Dividing by 100 first and multiply with 100 afterwards "shifts" two decimal places to the right and left so that math.ceil works on the hundreds. You could use 10**n instead of 100 if you want to round to tens (n = 1), thousands (n = 3), etc.

An alternative way to do this is to avoid floating point numbers (they have limited precision) and instead use integers only. Integers have arbitrary precision in Python, so this lets you round numbers of any size. The rule for rounding is simple: find the remainder after division with 100, and add 100 minus this remainder if it's non-zero:

Copy>>> def roundup(x):
...     return x if x % 100 == 0 else x + 100 - x % 100

This works for numbers of any size:

Copy>>> roundup(100)
100
>>> roundup(130)
200
>>> roundup(1234567891234567891)
1234567891234567900L

I did a mini-benchmark of the two solutions:

Copy$ python -m timeit -s 'import math' -s 'x = 130' 'int(math.ceil(x/100.0)) * 100'
1000000 loops, best of 3: 0.364 usec per loop
$ python -m timeit -s 'x = 130' 'x if x % 100 == 0 else x + 100 - x % 100'
10000000 loops, best of 3: 0.162 usec per loop

The pure integer solution is faster by a factor of two compared to the math.ceil solution.

Thomas proposed an integer based solution that is identical to the one I have above, except that it uses a trick by multiplying Boolean values. It is interesting to see that there is no speed advantage of writing the code this way:

Copy$ python -m timeit -s 'x = 130' 'x + 100*(x%100>0) - x%100'
10000000 loops, best of 3: 0.167 usec per loop

As a final remark, let me also note, that if you had wanted to round 101โ€“149 to 100 and round 150โ€“199 to 200, e.g., round to the nearest hundred, then the built-in round function can do that for you:

Copy>>> int(round(130, -2))
100
>>> int(round(170, -2))
200
2 of 11
49

This is a late answer, but there's a simple solution that combines the best aspects of the existing answers: the next multiple of 100 up from x is x - x % -100 (or if you prefer, x + (-x) % 100).

Copy>>> x = 130
>>> x -= x % -100  # Round x up to next multiple of 100.
>>> x
200

This is fast and simple, gives correct results for any integer x (like John Machin's answer) and also gives reasonable-ish results (modulo the usual caveats about floating-point representation) if x is a float (like Martin Geisler's answer).

Copy>>> x = 0.1
>>> x -= x % -100
>>> x
100.0
๐ŸŒ
Esri Community
community.esri.com โ€บ t5 โ€บ python-questions โ€บ round-to-nearest-100th โ€บ td-p โ€บ 453752
Round to nearest 100th - Python
August 28, 2013 - Solved: I am a little confused about how to round a field to the nearest 100th. Would I use math.ceil() or the round()? math.ceil([field]/100)*100 Does this only
๐ŸŒ
YouTube
youtube.com โ€บ watch
How to round to the nearest 10, 100, 1000 with Python
Enjoy the videos and music you love, upload original content, and share it all with friends, family, and the world on YouTube.
๐ŸŒ
Bobby Hadz
bobbyhadz.com โ€บ blog โ€บ python-round-number-to-nearest-100
Round a number to the nearest 5, 10, 100, 1000 in Python | bobbyhadz
Multiply the result by 100 to get the number rounded up to the nearest 100. Use the math.floor() method to round a number down to the nearest 100.
๐ŸŒ
Linux Tip
linuxscrew.com โ€บ home โ€บ programming โ€บ python โ€บ how to round numbers up/down/nearest in python
How to Round Numbers Up/Down/Nearest in Python
April 20, 2021 - This article will show you how to round numbers up, down, and to the nearest 1, 10 100 (or any number) in the Python programming language.
๐ŸŒ
Real Python
realpython.com โ€บ python-rounding
How to Round Numbers in Python โ€“ Real Python
December 7, 2024 - Then look at the digit d in the first decimal place of m. If d is less than 5, round m down to the nearest integer. Otherwise, round m up. Finally, shift the decimal point back p places by dividing m by 10แต–.
Find elsewhere
๐ŸŒ
TutorialKart
tutorialkart.com โ€บ python โ€บ python-round โ€บ python-round-to-nearest-10
Python - Round Number to Nearest 10
November 30, 2020 - To round to nearest 100, we need to provide -2 as second argument to round(). ... number = int(input('Enter a number :')) rounded = round(number, -2) print('Rounded Number :', rounded) ...
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ how-to-round-numbers-up-or-down-in-python
Python Round to Int โ€“ How to Round Up or Round Down to the Nearest Whole Number
May 24, 2022 - The code above is similar to the last example except for the second parameter. We passed in a value of two. This will round the number to the nearest hundredth (two decimal places).
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ round-function-python
round() function in Python - GeeksforGeeks
Python round() function 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 ...
Published ย  August 7, 2024
๐ŸŒ
Inspector
inspector.dev โ€บ home โ€บ round up numbers to integer in python โ€“ fast tips
Round Up Numbers to Integer in Python - Inspector.dev
June 17, 2025 - The math library in Python provides the ceil() and floor() functions to round numbers up and down, respectively, to the nearest integer. These functions are useful when you need to work with integer values, especially in cases like calculating ...
๐ŸŒ
InterServer
interserver.net โ€บ home โ€บ programming โ€บ python round() function explained: examples, decimals, and best practices
Python round() Function Explained: Examples, Decimals, and Best Practices - Interserver Tips
October 1, 2025 - You can round a number to two decimal ... when rounded to one or two decimals. You can also round numbers to the nearest 10, 100, or even 1000 by using negative ndigits....
๐ŸŒ
Mimo
mimo.org โ€บ glossary โ€บ python โ€บ round-function
Mimo: The coding platform you need to learn Web Development, Python, and more.
Using a negative value for the ndigits parameter, the round() function can round a floating-point number or integer to a multiple of 10. In the following example, we round 12345 to the nearest hundred (12300), simplifying the value. ... Pythonโ€™s round() function works well in many cases.
Top answer
1 of 2
5

A general purpose solution, this allows rounding to an arbitrary resolution (well, other than zero of course, but a resolution of zero makes little sense (a)). For your case, you just need to provide 0.02 as the resolution, though other values are possible, as shown in the test cases.

# This is the function you want.

def roundPartial (value, resolution):
    return round (value / resolution) * resolution

# All these are just test cases, the first two being your own test data.

print "Rounding to fiftieths"
print roundPartial (0.405, 0.02)
print roundPartial (0.412, 0.02)

print "Rounding to quarters"
print roundPartial (1.38, 0.25)
print roundPartial (1.12, 0.25)
print roundPartial (9.24, 0.25)
print roundPartial (7.76, 0.25)

print "Rounding to hundreds"
print roundPartial (987654321, 100)

This outputs:

Rounding to fiftieths
0.4
0.42
Rounding to quarters
1.5
1.0
9.25
7.75
Rounding to hundreds
987654300.0

(a) If you have the particular personality disorder that requires you to handle this possibility, just be aware that you're after the closest number that is a multiple of your desired resolution. Since the closest number to N (for any N) that is a multiple of 0 is always 0, you could modify the function as follows:

def roundPartial (value, resolution):
    if resolution == 0:
        return 0
    return round (value / resolution) * resolution

Alternatively, you could simply promise yourself not to pass zero as a resolution :-)

2 of 2
0

A small fix to the earlier solution:

def roundPartial (value, resolution):
    return round (float(value) / resolution) * resolution

Earlier

roundPartial(19, 10) = 10.0

With fix

roundPartial(19, 10) = 20.0
๐ŸŒ
Alteryx Community
community.alteryx.com โ€บ t5 โ€บ Alteryx-Designer-Desktop-Discussions โ€บ Round-up-To-The-Nearest-Hundred โ€บ td-p โ€บ 1081302
Solved: Round up To The Nearest Hundred - Alteryx Community
February 13, 2023 - Tried CEIL function but that just give me nearest integer. ... Solved! Go to Solution. ... Solved! Go to Solution. ... I would look at the ROUND function! Something like ROUND([Field],100) and putting some logic in to say anything under fifty needs to make sure to round up?