Python Forum

Full Version: Not rounding to desired decimal places?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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.
(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!