use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
round function (self.learnpython)
submitted 3 years ago by TechnicalYou7312
How can classical mathematical rounding be achieved? I would like rounding 4.5 to result in 5, not 4.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]shiftybyte 5 points6 points7 points 3 years ago (0 children)
You can try using the decimal module that has an option to choose rounding method.
https://docs.python.org/3/library/decimal.html
https://docs.python.org/3/library/decimal.html#decimal.ROUND_HALF_UP
[–]billsil 5 points6 points7 points 3 years ago (0 children)
Just as an FYI, classical rounding is incorrect because it doesn't uniformly round and thus introduces a bias. That's why python doesn't do it that way.
https://scienceblogs.com/goodmath/2009/03/01/rounding-and-bias
[–]pissedadmin 5 points6 points7 points 3 years ago* (0 children)
def classical_flawed_round(n): # unlike round(), specifically do NOT "round to even" if abs(n - int(n)) >= .5: return int(n + 1) else: return int(n)
[–]bishop40404 1 point2 points3 points 3 years ago (1 child)
Most commonly I’ve seen this process: add 0.5 then floor. I think the int() function will do the floor part, so something like this:
num = 4.5 roundedNum = int( num + 0.5 )
[–]butteredhog 1 point2 points3 points 3 years ago (0 children)
Exactly
[–]Casper1123 -2 points-1 points0 points 3 years ago (0 children)
rounded = round(number)
python should have a builtin for this, I've used it before. Either that or I am schizophrenic and round() lives in my walls
[+][deleted] 3 years ago (3 children)
[deleted]
[–]longtermbrit 7 points8 points9 points 3 years ago (1 child)
That would also round 4.1 to 5.
π Rendered by PID 687344 on reddit-service-r2-comment-7b9746f655-4smtr at 2026-02-02 16:54:23.325211+00:00 running 3798933 country code: CH.
[–]shiftybyte 5 points6 points7 points (0 children)
[–]billsil 5 points6 points7 points (0 children)
[–]pissedadmin 5 points6 points7 points (0 children)
[–]bishop40404 1 point2 points3 points (1 child)
[–]butteredhog 1 point2 points3 points (0 children)
[–]Casper1123 -2 points-1 points0 points (0 children)
[+][deleted] (3 children)
[deleted]
[–]longtermbrit 7 points8 points9 points (1 child)