Python Forum
Not rounding to desired decimal places? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Not rounding to desired decimal places? (/thread-32776.html)



Not rounding to desired decimal places? - pprod - Mar-05-2021

Hi,
I'm trying to get my head round this: I've rounded all items in a list (List A) to 5 decimal places and created a variable (List B) that takes the rounded numbers from List A and adds a constant. Upon exporting the lists to Excel, I realised that some values in list B have not been rounded to 5 decimal places as you can tell from the second row in the Excel spreadsheet (0.100469999999999). I don't understand why this happens as each item in List A is rounded, when adding 0.01 in List B shouldn't mess up the decimal places, right? Thanks!

  List_float = [float(i) for i in String] 
    List A = [round((i/2300), 5) for i in List_float] 
    List B = [i + 0.01 for i in List A]
When displaying 15 decimal places in Excel:
A 0.090470000000000
B 0.100469999999999
A 0.092480000000000
B 0.102480000000000


RE: Not rounding to desired decimal places? - Gribouillis - Mar-05-2021

Floating numbers are stored in binary format by the computer. Real numbers such as 0.1 that have a finite number of decimal digits may have an infinite number of binary digits. It means that they are not stored exactly and a rounding error may become visible in arithmetic operations. For example
>>> 0.10047 - (0.09047 + 0.01)
1.3877787807814457e-17
>>> 0.09047 * 100
9.046999999999999
See this manual page. Note that this limitation is not due to the Python language.


RE: Not rounding to desired decimal places? - pprod - Mar-05-2021

(Mar-05-2021, 11:04 AM)Gribouillis Wrote: Floating numbers are stored in binary format by the computer. Real numbers such as 0.1 that have a finite number of decimal digits may have an infinite number of binary digits. It means that they are not stored exactly and a rounding error may become visible in arithmetic operations. For example
>>> 0.10047 - (0.09047 + 0.01)
1.3877787807814457e-17
>>> 0.09047 * 100
9.046999999999999
See this manual page. Note that this limitation is not due to the Python language.


That was really helpful Gribouillis. Thanks!