Skip to content

Commit cb39d1f

Browse files
committed
Issue 19933: Provide default argument for ndigits in round. Patch by Vajrasky Kok.
1 parent 807b80d commit cb39d1f

File tree

4 files changed

+16
-4
lines changed

4 files changed

+16
-4
lines changed

Doc/library/functions.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,8 +1225,8 @@ are always available. They are listed here in alphabetical order.
12251225
.. function:: round(number[, ndigits])
12261226

12271227
Return the floating point value *number* rounded to *ndigits* digits after
1228-
the decimal point. If *ndigits* is omitted, it defaults to zero. Delegates
1229-
to ``number.__round__(ndigits)``.
1228+
the decimal point. If *ndigits* is omitted, it returns the nearest integer
1229+
to its input. Delegates to ``number.__round__(ndigits)``.
12301230

12311231
For the built-in types supporting :func:`round`, values are rounded to the
12321232
closest multiple of 10 to the power minus *ndigits*; if two multiples are

Lib/test/test_float.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,14 @@ def test(fmt, value, expected):
773773
test(sfmt, NAN, ' nan')
774774
test(sfmt, -NAN, ' nan')
775775

776+
def test_None_ndigits(self):
777+
for x in round(1.23), round(1.23, None), round(1.23, ndigits=None):
778+
self.assertEqual(x, 1)
779+
self.assertIsInstance(x, int)
780+
for x in round(1.78), round(1.78, None), round(1.78, ndigits=None):
781+
self.assertEqual(x, 2)
782+
self.assertIsInstance(x, int)
783+
776784

777785
# Beginning with Python 2.6 float has cross platform compatible
778786
# ways to create and represent inf and nan

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ Core and Builtins
3636
Library
3737
-------
3838

39+
- Issue 19933: Provide default argument for ndigits in round. Patch by
40+
Vajrasky Kok.
41+
3942
- Issue #23193: Add a numeric_owner parameter to
4043
tarfile.TarFile.extract and tarfile.TarFile.extractall. Patch by
4144
Michael Vogt and Eric Smith.

Objects/floatobject.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -986,8 +986,9 @@ float_round(PyObject *v, PyObject *args)
986986
x = PyFloat_AsDouble(v);
987987
if (!PyArg_ParseTuple(args, "|O", &o_ndigits))
988988
return NULL;
989-
if (o_ndigits == NULL) {
990-
/* single-argument round: round to nearest integer */
989+
if (o_ndigits == NULL || o_ndigits == Py_None) {
990+
/* single-argument round or with None ndigits:
991+
* round to nearest integer */
991992
rounded = round(x);
992993
if (fabs(x-rounded) == 0.5)
993994
/* halfway case: round to even */

0 commit comments

Comments
 (0)