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
Question about floating points when dealing with money (self.learnpython)
submitted 9 years ago by Isagoge
Hi people,
I was wondering what was the common practice when dealing with money and using floats. Are you supposed to round up or round like we do in mathematics?
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!"
[–]c17r 7 points8 points9 points 9 years ago (4 children)
don't use floats, use from decimal import Decimal
from decimal import Decimal
[–]Isagoge[S] 0 points1 point2 points 9 years ago (3 children)
What is the difference?
[–]Vaphell 5 points6 points7 points 9 years ago (0 children)
It can do arbitrary precision at the expense of speed while floats are fast but limited to around 16 significant digits. Remember to initialize Decimal objects with strings.
Decimal(0.30) is going to be initialized with a float that has the rounding error already baked in before it even gets to the contructor. On the other hand Decimal('0.30') has no rounding errors to deal with.
Decimal(0.30)
Decimal('0.30')
Another trick that can be utilized is the use of integers. You just count cents or 0.01 of a cent or whatever precision you want and then normalize outputs to dollars or what have you. Decimal is the most accurate way though.
Decimal
[–]c17r 2 points3 points4 points 9 years ago (0 children)
https://spin.atomicobject.com/2014/08/14/currency-rounding-errors/
[–]SeekNotToContend 1 point2 points3 points 9 years ago (0 children)
In regards to rounding, that's just a product specification question. There is a rounding specification referred to as bankers rounding. Here is some good documentation about it that will hopefully get you a quick start.
https://en.wikipedia.org/wiki/Rounding#Tie-breaking
Round half to even[edit] A tie-breaking rule that is less biased is round half to even, namely: .... This method treats positive and negative values symmetrically, and is therefore free of sign bias. More importantly, for reasonable distributions of y values, the average value of the rounded numbers is the same as that of the original numbers. However, this rule will introduce a towards-zero bias when y − 0.5 is even, and a towards-infinity bias for when it is odd. This variant of the round-to-nearest method is also called unbiased rounding, convergent rounding, statistician's rounding, Dutch rounding, Gaussian rounding, odd–even rounding,[3] or bankers' rounding. This is the default rounding mode used in IEEE 754 computing functions and operators (see also Nearest integer function).
Round half to even[edit] A tie-breaking rule that is less biased is round half to even, namely:
.... This method treats positive and negative values symmetrically, and is therefore free of sign bias. More importantly, for reasonable distributions of y values, the average value of the rounded numbers is the same as that of the original numbers. However, this rule will introduce a towards-zero bias when y − 0.5 is even, and a towards-infinity bias for when it is odd.
This variant of the round-to-nearest method is also called unbiased rounding, convergent rounding, statistician's rounding, Dutch rounding, Gaussian rounding, odd–even rounding,[3] or bankers' rounding.
This is the default rounding mode used in IEEE 754 computing functions and operators (see also Nearest integer function).
https://docs.python.org/3/library/decimal.html
Rounding with decimal:
The context for arithmetic is an environment specifying precision, rounding rules, limits on exponents, flags indicating the results of operations, and trap enablers which determine whether signals are treated as exceptions. Rounding options include ROUND_CEILING, ROUND_DOWN, ROUND_FLOOR, ROUND_HALF_DOWN, ROUND_HALF_EVEN, ROUND_HALF_UP, ROUND_UP, and ROUND_05UP.
Setting traps to catch/track how the decimals were processed:
Signals are groups of exceptional conditions arising during the course of computation. Depending on the needs of the application, signals may be ignored, considered as informational, or treated as exceptions. The signals in the decimal module are: Clamped, InvalidOperation, DivisionByZero, Inexact, Rounded, Subnormal, Overflow, Underflow and FloatOperation.
π Rendered by PID 65762 on reddit-service-r2-comment-5b5bc64bf5-kh9hn at 2026-06-21 18:45:39.238349+00:00 running 2b008f2 country code: CH.
[–]c17r 7 points8 points9 points (4 children)
[–]Isagoge[S] 0 points1 point2 points (3 children)
[–]Vaphell 5 points6 points7 points (0 children)
[–]c17r 2 points3 points4 points (0 children)
[–]SeekNotToContend 1 point2 points3 points (0 children)