0

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.

AnonyMouse
  • 432
  • 8
  • 24
  • 1
    I don't believe its a quirk with python, it's a quirk with how decimal numbers are stored in computers – ritlew Jul 01 '16 at 20:52
  • 2
    Note that Decimal precision refers to the number of significant figures, not the number of decimal places. – Daniel Roseman Jul 01 '16 at 20:53
  • Cheers @DanielRoseman, I didn't know that. – AnonyMouse Jul 01 '16 at 20:56
  • 1
    To help us understand what you need: Why doesn't "use decimal" work for you? You can cast floats to Decimals, perform your rounding (which will now be perfect) then use those numbers as numbers. If need be, you can even cast them back to floats again at the end (although this will then incur the rounding errors that are a consequence of all floating point operations on computers again.) – Jonathan Hartley Jul 01 '16 at 21:16
  • 1
    Thanks so much @JonathanHartley! I was in the mindset where I stopped thinking about using round because `round(Decimal(x),2)` didn't return the desired result, but I investigated by doing it in two steps after your suggestion, and the problem is solved. You're a star! Cheers :)))) – AnonyMouse Jul 01 '16 at 21:51

0 Answers0