round() function #13080
Replies: 4 comments 2 replies
-
Try 3.26 |
Beta Was this translation helpful? Give feedback.
-
FYI this matches CPython:
e.g. pyboard (single-precision float)
e.g. micropython on linux (double-precision float)
Are you aware of Python's rounding rules (which is round-to-even rather than round-up for 0.5). Also be aware of floating point precision issues. Please see https://docs.python.org/3/library/functions.html#round for more details. |
Beta Was this translation helpful? Give feedback.
-
I guess you expected financial rounding. You don't get this in programming languages because the error is bigger, if you sum up financial rounded values. The scientific rounding is used in programming languages. def financial_round(value):
fraction = value - int(value)
if fraction >= 0.5:
return int(value) + 1
else:
return int(value)
def gen_seq():
for x in range(1, 11):
yield x + 0.5
x = sum(gen_seq())
y = sum(round(x) for x in gen_seq())
z = sum(financial_round(x) for x in gen_seq())
diff1 = abs(x - y)
diff2 = abs(x - z) |
Beta Was this translation helpful? Give feedback.
-
Hi, def round(val, nbDec): which gives good results in all cases, even with negative nbDec (Number of decimals). In fact, the problem is not there. Regards |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello,
I have a question concerning the "round()" function. round(3.25,1) should return 3.3 but it returns 3.2. Why ?
Beta Was this translation helpful? Give feedback.
All reactions