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
Answer from Martin Geisler on Stack Overflow
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
🌐
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.

🌐
Esri Community
community.esri.com › t5 › python-questions › round-to-nearest-100th › td-p › 453752
Round to nearest 100th - Python
August 28, 2013 - import arcpy import os, math from arcpy import env in_workspace = r"F:/Inventory_export_dev/testRnd" fcList = [] for dirpath, dirnames, filenames in arcpy.da.Walk(in_workspace, datatype="FeatureClass",type="All"): for filename in filenames: arcpy.AddField_management(os.path.join(dirpath, filename), fieldName, "SHORT", "", "", "", "", "NULLABLE") arcpy.CalculateField_management(os.path.join(dirpath, filename), "SL_ELEV", "int(round( !ELEVATION!, -2))", "PYTHON") No need to put the filenames in a list if all you are going to do is iterate through them again. Just as well do the work on the first iteration. Of course, this will errror out if any of the FC's in your in_workspace do not have the numeric ELEVATION field. R_ ... math.ceil() always rounds up. math.floor() rounds down. round() rounds the the nearest integer or to a particular decimal place if the second parameter is specified.
🌐
Real Python
realpython.com › python-rounding
How to Round Numbers in Python – Real Python
December 7, 2024 - To implement the rounding up strategy in Python, you’ll use the ceil() function from the math module. The ceil() function gets its name from the mathematical term ceiling, which describes the nearest integer that’s greater than or equal ...
🌐
Bobby Hadz
bobbyhadz.com › blog › python-round-number-to-nearest-100
Round a number to the nearest 5, 10, 100, 1000 in Python | bobbyhadz
Use the `round()` function to round a number to the nearest 100, e.g. `result = round(num, -2)`.
🌐
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.
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) ...
🌐
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.
🌐
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?
🌐
W3Schools
w3schools.com › python › ref_func_round.asp
Python round() Function
The default number of decimals is 0, meaning that the function will return the nearest integer. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com
🌐
Server Academy
serveracademy.com › blog › python-round-function-tutorial
Python Round() Function Tutorial - Server Academy
For these scenarios, you can use Python’s math library. The math.ceil() function rounds a number up to the nearest integer, regardless of its decimal value.
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-numbers-to-the-nearest-100 › td-p › 580108
Solved: Round numbers to the nearest 100 - Alteryx Community
June 1, 2020 - Since you have multiple numeric columns you want to round, you could use a Multi Field formula tool, and the ROUND formula with a formula something like ROUND([_CurrentField_],100), selecting the numeric fields.
🌐
Upgrad
upgrad.com › home › blog › data science › understanding python round function: guide to precise rounding
Round Function in Python [2025] – Explained for Beginners
October 8, 2025 - If it’s negative, the rounding happens to the left of the decimal (for example, to the nearest ten or hundred). From foundational certifications to advanced degrees, gain cutting-edge skills in Generative AI, Machine Learning, and Data Analysis. Enrol now and lead the change. Master’s Degree in Artificial Intelligence and Data Science From OPJGU · Generative AI Mastery Certificate for Data Analysis ... In the first line, the number 12.567 is rounded to two decimal places. In the second line, Python rounds it to the nearest integer because the ndigits argument isn’t specified.
🌐
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).