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
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);
    }
}
🌐
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…
🌐
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 ...
🌐
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.
🌐
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 ☺

🌐
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 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....
🌐
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.
🌐
Reddit
reddit.com › r/learnpython › round() fuction not working properly
round() fuction not working properly : r/learnpython
May 30, 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!
🌐
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:
🌐
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 ...
🌐
Medium
vasusharma7.medium.com › cpython-round-2dcdc83f0330
The internals of cpython round() function's implementation ! | Medium
November 19, 2024 - One thing to note before we dive into the code would be that anything we receive from the userland python code into the C code is not mapped to primitive datatypes in C. For instance, the simple python number received from user as number of digits to round off to is mapped to Py_Number internally and similarly the input float is mapped to PyFloat internally. ... This function begings by converting the floating number received from userland python code as a PyObjectto double datatype in C.
🌐
Sololearn
sololearn.com › en › Discuss › 1428917 › how-to-use-round-function-in-c-language
How to use round function in c language? | Sololearn: Learn to code for FREE!
You can find an explanation of these functions, as well as code examples in this site: http://www.cplusplus.com/reference/cmath/round/ Good coding. ... Ребят хочу сделать игру, предложите идею и Помогите её сделать🙃 ... I need help I don't know how to use python ...
🌐
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 ...