I'm trying to round in Python, but I can't get the correct result each time, which should be
[3.24, -0.16, 0.24, 0.36]
First, I used round(number, 2)
[3.24, -0.16000000000000003, 0.24, 0.36]
The second number in the list is not rounded whatsoever...
Then, I used decimal
from decimal import *
getcontext().prec = 2
[Decimal('3.2'), Decimal('-0.16'), Decimal('0.24'), Decimal('0.36')]
This fixes the original errant number, but now the first number in the list is not rounded correctly, it should be 3.24
.
So, I tried a precision of three out of curiosity to see what happens
from decimal import *
getcontext().prec = 3
[Decimal('3.24'), Decimal('-0.160'), Decimal('0.240'), Decimal('0.360')]
... now the first item in the list is doing in three-digit precision what it was supposed to do with two-digit precision.
So I tried, based on another answer, to use math.ceil
math.ceil(number*100)/100 for number in list_of_numbers]
[3.24, -0.16, 0.25, 0.37]
Now the first two numbers in the list are perfect, but the last two numbers in the list, which were supposed to be 24
and 36
, are now 25
and 37
.
Is it a fool's errand to try and get them all to be exactly right at the same time?
Edit due being marked as duplicate
I'm asking if there's a fix for this. It's not just a question on Python and its quirks. I've seen other answers and they say use decimal, which doesn't work for me, or to format it and output it as a string, but I don't want a string, I need to use these numbers further, as numbers.