This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: clarify docs about 'round()' accepting a negative integer for ndigits
Type: Stage: resolved
Components: Documentation Versions: Python 3.7, Python 3.6, Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: docs@python Nosy List: ChrisRands, Gerrit.Holl, Mariatta, cheryl.sabella, docs@python, mark.dickinson, veky, vstinner
Priority: normal Keywords:

Created on 2017-02-28 12:30 by ChrisRands, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 357 merged Gerrit.Holl, 2017-02-28 12:53
PR 862 merged Mariatta, 2017-03-27 23:56
PR 863 merged Mariatta, 2017-03-27 23:57
PR 877 merged cheryl.sabella, 2017-03-28 16:22
PR 892 merged Mariatta, 2017-03-30 02:05
PR 893 merged Mariatta, 2017-03-30 02:05
Messages (18)
msg288710 - (view) Author: (ChrisRands) * Date: 2017-02-28 12:30
With a negative integer for ndigits the output from 'round()', for example 'round(3, -2)', is always zero ('0' for 'int.__round__' and '0.0' or '-0.0' for 'float.__round__').

I think either it should raise an exception or the docs should be updated to reflect the current behavior.

The docs are currently silent on this: https://docs.python.org/3/library/functions.html#round

I don't know C, but there appears to be a note in the source about this implying it is the desired behavior but without an explanation: "For ndigits < NDIGITS_MIN, x always rounds to +-0.0.": https://github.com/python/cpython/blob/6f0eb93183519024cb360162bdd81b9faec97ba6/Objects/floatobject.c

Anyway, I can't imagine an actual use case for negative ndigits.
msg288711 - (view) Author: Gerrit Holl (Gerrit.Holl) * Date: 2017-02-28 12:34
>>> round(21345, -2)
21300

Desired and useful to me.
msg288713 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2017-02-28 12:38
Yep, it's (genuinely) a feature, not a bug, and being able to round to the nearest ten, hundred, thousand, etc. is useful. The docs could perhaps be improved to make it clearer that this is supported.
msg288714 - (view) Author: (ChrisRands) * Date: 2017-02-28 12:45
Ah yes, you're both completely right of course. Perhaps the docs could still be clarified though. I managed to completely overlook this, and perhaps other non-expert Python users would too.
msg288715 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2017-02-28 12:50
Yes, I think the initial description could be clarified. It currently reads:

> Return the floating point value number rounded to ndigits digits after the decimal point.

... which doesn't make it clear what happens for negative ndigits. 

There's a line in the second paragraph:

> values are rounded to the closest multiple of 10 to the power minus ndigits

which technically *does* cover the case of negative ndigits correctly, but I think it would be good to have a clarification in the first paragraph of the description.
msg288751 - (view) Author: Vedran Čačić (veky) * Date: 2017-03-01 09:38
This is one of those things that show Python being extremely consistent within itself. Of course, docs should be clarified if needed, but the feature is desirable and useful.
msg290644 - (view) Author: Cheryl Sabella (cheryl.sabella) * (Python committer) Date: 2017-03-27 20:54
I'm new to contributing.  Can I work on this one?
msg290662 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2017-03-27 22:15
New changeset 6003db7db5fec545c01923c198a5fdfca5a91538 by Victor Stinner (Gerrit Holl) in branch 'master':
bpo-29677: DOC: clarify documentation for `round` (#357)
https://github.com/python/cpython/commit/6003db7db5fec545c01923c198a5fdfca5a91538
msg290669 - (view) Author: Mariatta (Mariatta) * (Python committer) Date: 2017-03-28 00:02
New changeset 596506216104613591218c2896cdb49fa0b7e5bb by Mariatta in branch '3.6':
bpo-29677: DOC: clarify documentation for `round` (GH-357) (GH-862)
https://github.com/python/cpython/commit/596506216104613591218c2896cdb49fa0b7e5bb
msg290670 - (view) Author: Mariatta (Mariatta) * (Python committer) Date: 2017-03-28 00:09
New changeset 8994f36287b068e8ea9a9230fc90eda72bf5fff0 by Mariatta in branch '3.5':
bpo-29677: DOC: clarify documentation for `round` (GH-357) (GH-863)
https://github.com/python/cpython/commit/8994f36287b068e8ea9a9230fc90eda72bf5fff0
msg290671 - (view) Author: Mariatta (Mariatta) * (Python committer) Date: 2017-03-28 00:19
Cheryl, thank you for your interest in working on this issue.

The original issue has been addressed in Gerrit Holl's PR. But it seems like the docs can be further improved.

Here is Victor's comment about it: (copied from GH-357)

```
while we are modifying the doc, I dislike the start: "Return the floating point value number": number can be an integer. round(123, -2) doesn't use floating point numbers. Maybe rephrase to following text?

Return number rounded to (...). number can be an integer or a floating point number.

Or just remove "floating pointer number"?
```

Will you be interested in preparing a PR to address Victor's comment above?

Thanks :)
msg290672 - (view) Author: Mariatta (Mariatta) * (Python committer) Date: 2017-03-28 00:22
GH-357 is:
https://github.com/python/cpython/pull/357/files

:)
msg290694 - (view) Author: Cheryl Sabella (cheryl.sabella) * (Python committer) Date: 2017-03-28 10:13
Mariatta,

Yes, thank you, I will work on a pull request to address Victor's comment.  Since it's my first time, I hope to read through the contributing docs and get this done today or tomorrow.

Thank you!
msg290793 - (view) Author: Mariatta (Mariatta) * (Python committer) Date: 2017-03-29 21:14
New changeset 85deefcf61d3cc192846f41a4ccc6df17da60c98 by Mariatta (csabella) in branch 'master':
bpo-29677: DOC: clarify documentation for `round` (GH-877)
https://github.com/python/cpython/commit/85deefcf61d3cc192846f41a4ccc6df17da60c98
msg290794 - (view) Author: Cheryl Sabella (cheryl.sabella) * (Python committer) Date: 2017-03-29 22:29
Mariatta,

Thank you so much for your support and encouragement while I worked on my first PR.  You've made this an awesome experience!
msg290802 - (view) Author: Mariatta (Mariatta) * (Python committer) Date: 2017-03-30 02:10
New changeset 2609c9ee78c53d501914a5a90dbe094d9a8c3c97 by Mariatta in branch '3.6':
bpo-29677: DOC: clarify documentation for `round` (GH-877) (GH-892)
https://github.com/python/cpython/commit/2609c9ee78c53d501914a5a90dbe094d9a8c3c97
msg290803 - (view) Author: Mariatta (Mariatta) * (Python committer) Date: 2017-03-30 02:10
New changeset eef6e11f9883f54de491121b8c49fdadd3e3506d by Mariatta in branch '3.5':
bpo-29677: DOC: clarify documentation for `round` (GH-877) (GH-893)
https://github.com/python/cpython/commit/eef6e11f9883f54de491121b8c49fdadd3e3506d
msg290804 - (view) Author: Mariatta (Mariatta) * (Python committer) Date: 2017-03-30 02:16
Gerrit, Cheryl, thanks and congrats for your first contributions to CPython!

They've been merged and backported to 3.5 and 3.6.
So I'm closing this now.

Thanks all :)
History
Date User Action Args
2022-04-11 14:58:43adminsetgithub: 73863
2017-03-30 02:16:02Mariattasetstatus: open -> closed
resolution: fixed
messages: + msg290804

stage: backport needed -> resolved
2017-03-30 02:10:24Mariattasetmessages: + msg290803
2017-03-30 02:10:09Mariattasetmessages: + msg290802
2017-03-30 02:05:31Mariattasetpull_requests: + pull_request796
2017-03-30 02:05:12Mariattasetpull_requests: + pull_request795
2017-03-29 22:29:32cheryl.sabellasetmessages: + msg290794
2017-03-29 21:15:20Mariattasetstage: needs patch -> backport needed
2017-03-29 21:14:09Mariattasetmessages: + msg290793
2017-03-28 16:22:32cheryl.sabellasetpull_requests: + pull_request776
2017-03-28 10:13:49cheryl.sabellasetmessages: + msg290694
2017-03-28 00:22:05Mariattasetmessages: + msg290672
2017-03-28 00:19:46Mariattasetstage: needs patch
messages: + msg290671
versions: - Python 2.7
2017-03-28 00:09:23Mariattasetmessages: + msg290670
2017-03-28 00:02:31Mariattasetnosy: + Mariatta
messages: + msg290669
2017-03-27 23:57:16Mariattasetpull_requests: + pull_request763
2017-03-27 23:56:38Mariattasetpull_requests: + pull_request762
2017-03-27 22:15:22vstinnersetnosy: + vstinner
messages: + msg290662
2017-03-27 21:39:51Mariattasetversions: + Python 2.7, Python 3.5, Python 3.6, Python 3.7
2017-03-27 20:54:00cheryl.sabellasetnosy: + cheryl.sabella
messages: + msg290644
2017-03-02 18:57:59brett.cannonsettitle: 'round()' accepts a negative integer for ndigits -> clarify docs about 'round()' accepting a negative integer for ndigits
2017-03-01 09:38:27vekysetnosy: + veky
messages: + msg288751
2017-02-28 12:53:06Gerrit.Hollsetpull_requests: + pull_request304
2017-02-28 12:50:19mark.dickinsonsetmessages: + msg288715
2017-02-28 12:45:52ChrisRandssetmessages: + msg288714
2017-02-28 12:38:42mark.dickinsonsetnosy: + docs@python, mark.dickinson
messages: + msg288713

assignee: docs@python
components: + Documentation
2017-02-28 12:34:51Gerrit.Hollsetnosy: + Gerrit.Holl
messages: + msg288711
2017-02-28 12:30:24ChrisRandscreate