]> granicus.if.org Git - python/commitdiff
Issue 19933: Provide default argument for ndigits in round. Patch by Vajrasky Kok.
authorSteve Dower <steve.dower@microsoft.com>
Wed, 15 Apr 2015 20:10:59 +0000 (16:10 -0400)
committerSteve Dower <steve.dower@microsoft.com>
Wed, 15 Apr 2015 20:10:59 +0000 (16:10 -0400)
Doc/library/functions.rst
Lib/test/test_float.py
Misc/NEWS
Objects/floatobject.c

index 456dd098219a9ddf4b73f0126a7ba9a5ba725a85..e982ceb0b90e7100d21bcb9f66e487088faa3510 100644 (file)
@@ -1225,8 +1225,8 @@ are always available.  They are listed here in alphabetical order.
 .. function:: round(number[, ndigits])
 
    Return the floating point value *number* rounded to *ndigits* digits after
-   the decimal point.  If *ndigits* is omitted, it defaults to zero. Delegates
-   to ``number.__round__(ndigits)``.
+   the decimal point.  If *ndigits* is omitted, it returns the nearest integer
+   to its input.  Delegates to ``number.__round__(ndigits)``.
 
    For the built-in types supporting :func:`round`, values are rounded to the
    closest multiple of 10 to the power minus *ndigits*; if two multiples are
index 071d2173703f7ebe426658bac795e80e9a19b115..4251090522ed35466d3c55895f03846608f2b52c 100644 (file)
@@ -773,6 +773,14 @@ class RoundTestCase(unittest.TestCase):
             test(sfmt, NAN, ' nan')
             test(sfmt, -NAN, ' nan')
 
+    def test_None_ndigits(self):
+        for x in round(1.23), round(1.23, None), round(1.23, ndigits=None):
+            self.assertEqual(x, 1)
+            self.assertIsInstance(x, int)
+        for x in round(1.78), round(1.78, None), round(1.78, ndigits=None):
+            self.assertEqual(x, 2)
+            self.assertIsInstance(x, int)
+
 
 # Beginning with Python 2.6 float has cross platform compatible
 # ways to create and represent inf and nan
index c7584b2e2eec9827d003e2b4ad0b24292133947c..5f5cac6c35090799aa076cb09e7574ba755d2992 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -36,6 +36,9 @@ Core and Builtins
 Library
 -------
 
+- Issue 19933: Provide default argument for ndigits in round. Patch by
+  Vajrasky Kok.
+
 - Issue #23193: Add a numeric_owner parameter to
   tarfile.TarFile.extract and tarfile.TarFile.extractall. Patch by
   Michael Vogt and Eric Smith.
index 1d369f98beea8ff4ff79c4c2a375cfadc8281206..d6819814ef8334d93c0d175340b9b10da1afeaa2 100644 (file)
@@ -986,8 +986,9 @@ float_round(PyObject *v, PyObject *args)
     x = PyFloat_AsDouble(v);
     if (!PyArg_ParseTuple(args, "|O", &o_ndigits))
         return NULL;
-    if (o_ndigits == NULL) {
-        /* single-argument round: round to nearest integer */
+    if (o_ndigits == NULL || o_ndigits == Py_None) {
+        /* single-argument round or with None ndigits:
+         * round to nearest integer */
         rounded = round(x);
         if (fabs(x-rounded) == 0.5)
             /* halfway case: round to even */