just expanding the comment from Mark Dickinson and to make sure I understand it myself, the CPython round function is spread over several parts of the code base.

round(number, ndigits) starts by looking up and invoking the __round__ method on the object. this is implemented by the C function builtin_round_impl in bltinmodule.c

for floats this invokes the float.__round__ method, which is implemented in float___round___impl in floatobject.c:1045 but there's a stub entry point in floatobject.c.h that I think is mostly maintained by Python's argument clinic tool. this header is also where its PyMethodDef is defined as FLOAT___ROUND___METHODDEF

the C function float___round___impl starts by checking if ndigits was not specified (i.e. nothing passed, or passed as None), in this case then it calls round from the C standard library (or the version from pymath.c as a fallback).

if ndigits is specified then it probably calls the version of double_round in floatobject.c:927. this works in 53bit precision, so adjusts floating point rounding modes and is generally pretty fiddly code, but basically it converts the double to a string with a given precision, and then converts back to a double

for a small number of platforms there's another version of double_round at floatobject.c:985 that does the obvious thing of basically round(x * 10**ndigits) / 10**ndigits, but these extra operations can reduce precision of the result

note that the higher precision version will give different answers to the version in NumPy and equivalent version in R, as commented on here. for example, round(0.075, 2) results in 0.07 with the builtin round, while numpy and R give 0.08. the easiest way I've found of seeing what's going on is by using the decimal module to see the full decimal expansion of the float:

from decimal import Decimal

print(Decimal(0.075))

gives: 0.0749999999999999972…, i.e. 0.075 can't be accurately represented by a (binary) floating point number and the closest number happens to be slightly smaller, and hence it rounds down to 0.07. while the implementation in numpy gives 0.08 because it effectively does round(0.075 * 100) / 100 and the intermediate value happens to round up, i.e:

print(Decimal(0.075 * 100))

giving exactly 7.5, which rounds exactly to 8.

Answer from Sam Mason on Stack Overflow
🌐
W3Schools
w3schools.com › python › ref_func_round.asp
Python round() Function
Python Examples Python Compiler ... Certificate Python Training ... The round() function returns a floating point number that is a rounded version of the specified ......
🌐
W3Schools
w3schools.com › c › ref_math_round.php
C Math round() Function
The round() function is defined in the <math.h> header file. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
Discussions

Python Round() Function
Found an answer on https://www.programiz.com/python-programming/methods/built-in/round Note: The behavior of round() for floats can be surprising. Notice round(2.675, 2) gives 2.67 instead of the expected 2.68. This is not a bug: it's a result of the fact that most decimal fractions can't be represented exactly as a float. When the decimal 2.675 is converted to a binary floating-point number, it's again replaced with a binary approximation, whose exact value is: 2.67499999999999982236431605997495353221893310546875 Due to this, it is rounded down to 2.67. If you're in a situation where this precision is needed, consider using the decimal module, which is designed for floating-point arithmetic: from decimal import Decimal # normal float num = 2.675 print(round(num, 2)) # using decimal.Decimal (passed float as string for precision) num = Decimal('2.675') print(round(num, 2)) Hope this helps! More on reddit.com
🌐 r/learnpython
7
1
July 9, 2021
Round function should be improved
the round function should be improved, the round function is supposed to round up numbers that are in a decimal format. I have found that the round function only looks at the first decimal point, for example when I enter… More on discuss.python.org
🌐 discuss.python.org
0
0
July 5, 2024
round() fuction not working properly
Sounds like classic floating point math weirdness to me. More on reddit.com
🌐 r/learnpython
16
1
May 28, 2024
Rounding in C
Have you considered simply trying the code out? More on reddit.com
🌐 r/C_Programming
37
0
May 8, 2024
Top answer
1 of 2
5

just expanding the comment from Mark Dickinson and to make sure I understand it myself, the CPython round function is spread over several parts of the code base.

round(number, ndigits) starts by looking up and invoking the __round__ method on the object. this is implemented by the C function builtin_round_impl in bltinmodule.c

for floats this invokes the float.__round__ method, which is implemented in float___round___impl in floatobject.c:1045 but there's a stub entry point in floatobject.c.h that I think is mostly maintained by Python's argument clinic tool. this header is also where its PyMethodDef is defined as FLOAT___ROUND___METHODDEF

the C function float___round___impl starts by checking if ndigits was not specified (i.e. nothing passed, or passed as None), in this case then it calls round from the C standard library (or the version from pymath.c as a fallback).

if ndigits is specified then it probably calls the version of double_round in floatobject.c:927. this works in 53bit precision, so adjusts floating point rounding modes and is generally pretty fiddly code, but basically it converts the double to a string with a given precision, and then converts back to a double

for a small number of platforms there's another version of double_round at floatobject.c:985 that does the obvious thing of basically round(x * 10**ndigits) / 10**ndigits, but these extra operations can reduce precision of the result

note that the higher precision version will give different answers to the version in NumPy and equivalent version in R, as commented on here. for example, round(0.075, 2) results in 0.07 with the builtin round, while numpy and R give 0.08. the easiest way I've found of seeing what's going on is by using the decimal module to see the full decimal expansion of the float:

from decimal import Decimal

print(Decimal(0.075))

gives: 0.0749999999999999972…, i.e. 0.075 can't be accurately represented by a (binary) floating point number and the closest number happens to be slightly smaller, and hence it rounds down to 0.07. while the implementation in numpy gives 0.08 because it effectively does round(0.075 * 100) / 100 and the intermediate value happens to round up, i.e:

print(Decimal(0.075 * 100))

giving exactly 7.5, which rounds exactly to 8.

2 of 2
1

The source seems to be: https://github.com/python/cpython/blob/master/Python/pymath.c

double
round(double x)
{
    double absx, y;
    absx = fabs(x);
    y = floor(absx);
    if (absx - y >= 0.5)
        y += 1.0;
    return copysign(y, x);
}

where copysign is:

double
copysign(double x, double y)
{
    /* use atan2 to distinguish -0. from 0. */
    if (y > 0. || (y == 0. && atan2(y, -1.) > 0.)) {
        return fabs(x);
    } else {
        return -fabs(x);
    }
}
🌐
Programiz
programiz.com › python-programming › methods › built-in › round
Python round()
Become a certified Python programmer. Try Programiz PRO! ... The round() function rounds a number.
🌐
Server Academy
serveracademy.com › blog › python-round-function-tutorial
Python Round() Function Tutorial - Server Academy
Rounding numbers is a common operation in Python, especially when working with floating-point numbers. Python’s round() function makes it easy to round numbers to a specified number of decimal places or the nearest integer. Whether you need to round up, round down, or round to a specific decimal point, Python…
🌐
Unstop
unstop.com › home › blog › python round() function | syntax, errors, uses & more (+codes)
Python round() Function | Syntax, Errors, Uses & More (+Codes)
February 3, 2025 - Object-Oriented Programming (OOP) Concepts In Python ... Arbitrary Arguments Vs. Keyword Arguments ... The round() function in Python rounds a floating-point number to the nearest integer or a specified number of decimal places.
🌐
GeeksforGeeks
geeksforgeeks.org › round-function-python
round() function in Python - GeeksforGeeks
One of the common uses of rounding functions is Handling the mismatch between fractions and decimals. We usually work with just two or three digits to the right of the decimal point when there is no exact equivalent to the fraction in decimal. ... Note: In Python, if we round off numbers to floor or ceil without giving the second parameter, it will return 15.0 for example and in Python 3 it returns 15, so to avoid this we can use (int) type conversion in Python.
Published   August 7, 2024
Find elsewhere
🌐
Graduateschool
graduateschool.edu › the round function and its application in python
The Round Function and Its Application in Python - Free Video Tutorial
February 13, 2025 - This lesson is a preview from our Data Science & AI Certificate Online (includes software) and Python Certification Online (includes software &amp; exam). Enroll in a course for detailed lessons, live instructor support, and project-based training. Now, rounding—there's the round function that takes a float and a number of decimal places, or it could just take a float.
🌐
Medium
medium.com › @ElizavetaGorelova › rounding-in-python-choosing-the-best-way-c20c2a37fe29
Rounding in Python: Choosing The Best Way | by Elizaveta Gorelova | Medium
March 15, 2024 - Let’s talk about each function in more detail. But first, we import the math module into our program by writing the import math command at the beginning of the file. If we skip this step, Python simply won’t understand how to perform these functions. The ceil() function rounds the result up.
🌐
TutorialsPoint
tutorialspoint.com › c_standard_library › c_function_round.htm
C Standard Library: round Function
Python TechnologiesDatabasesComputer ... View All Categories ... The C library round() function can be used to calculate the floating-point into the nearest integer....
🌐
Reddit
reddit.com › r/learnpython › python round() function
r/learnpython on Reddit: Python Round() Function
July 9, 2021 -

Greetings,

Code: x = round(7.85, 2) print(x)

Result: 7.8

Why is that? Rounding down starts at 0 and ends at 4, and rounding up begins at 5 and ends at 9. The result should be 7.9.

Does Python have its own math rules? If so, why? Math is math...

Please and thank you ☺

🌐
Real Python
realpython.com › python-rounding
How to Round Numbers in Python – Real Python
December 7, 2024 - To round numbers to specific decimal places, you can use the round() function with a second argument specifying the number of decimals. For more advanced rounding strategies, you can explore Python’s decimal module or use NumPy and pandas ...
🌐
Python.org
discuss.python.org › python help
Round function should be improved - Python Help - Discussions on Python.org
July 5, 2024 - the round function should be improved, the round function is supposed to round up numbers that are in a decimal format. I have found that the round function only looks at the first decimal point, for example when I enter this: print(round(1.45), round(1.54)) as a result I get: 1 2 I have created some new code that works as the round function is supposed to: def rounding(num): #this code was made by anon #this looks to see if the num is a decimal if num % 1 != 0: #sees ...
🌐
Scaler
scaler.com › home › topics › round function in python
round() Function in Python | Scaler Topics
December 3, 2023 - Integer values are not much affected by the round function. Integers are affected only if the value of the n_digits parameter is less than zero. In such cases, the integer number gets less significant. For example, if 652 is rounded off to -1 decimal points, it would become 650. Let's have a look at some more examples with the help of the code below: Round off in python with a positive value of n_digits has no effect on the integer number.
🌐
Real Python
realpython.com › ref › builtin-functions › round
round() | Python’s Built-in Functions – Real Python
In this tutorial, you'll learn what kinds of mistakes you might make when rounding numbers and how you can best manage or avoid them. It's a great place to start for the early-intermediate Python developer interested in using Python for finance, data science, or scientific computing. ... By Leodanis Pozo Ramos • Updated Feb. 3, 2026 • Reviewed by Dan Bader ... Get a Python Cheat Sheet (PDF) and learn the basics of Python, like working with data types, dictionaries, lists, and Python functions:
🌐
Reddit
reddit.com › r/learnpython › round() fuction not working properly
round() fuction not working properly : r/learnpython
May 28, 2024 - I see... I don't really have a solution for that, but I'll search a bit, and if I found anything, I'll come back with more info! ... I have news! If you pass the round function as a tuple (e.g round(var, n)), it'll round to that amount of numbers!
🌐
Mimo
mimo.org › glossary › python › round-function
Python round(): Rounding Numbers in Python
Master Python from basics to advanced topics, including data structures, functions, classes, and error handling ... Start your coding journey with Python. Learn basics, data types, control flow, and more ... 1. Round to the Nearest Integer: When called with one argument, round() returns the ...
🌐
Software Testing Help
softwaretestinghelp.com › home › python › python round function: rounding numbers in python
Python Round Function: Rounding Numbers in Python
April 1, 2025 - The number after the decimal point, in this case, is 7, which falls between 5 and 9. Hence, we round up by adding 1 to 6. Finally, we discard all numbers after the decimal point and bring them back to their normal position, which gives us 3.457 · One of the built-in functions provided by Python to ...
🌐
Reddit
reddit.com › r/c_programming › rounding in c
r/C_Programming on Reddit: Rounding in C
May 8, 2024 -

I have a question when it comes to rounding in C. Does it round up or down at .5? If it does round up, then does that mean that the smallest value of k in the code below can only be 1?

 int main()
{
    int k = 13;
    int i;
    for (i = 0; i < 8; i++) {
        printf("%d", (k%2));
        k >>= 1;
    }
    printf("%n");
}

🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-round-numbers-in-python
How to Round Numbers in Python? - GeeksforGeeks
July 15, 2025 - Python provides various methods ... this article, we'll cover the most commonly used techniques for rounding numbers in Python, along with examples. For example, Input is 3.5 then Output should be 4. Python’s built-in round() function rounds a number to a given ...