Python Forum
Not rounding to desired decimal places?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Not rounding to desired decimal places?
#1
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
Reply
#2
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.
pprod likes this post
Reply
#3
(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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  need help rounding joseph202020 7 1,257 Feb-21-2023, 08:13 PM
Last Post: joseph202020
  Json filter is not capturing desired key/element mrapple2020 1 1,073 Nov-24-2022, 09:22 AM
Last Post: ibreeden
  from numpy array to csv - rounding SchroedingersLion 6 2,063 Nov-14-2022, 09:09 PM
Last Post: deanhystad
  how can I display only desired items? 3lnyn0 5 1,973 Dec-25-2021, 06:49 PM
Last Post: menator01
  Calculate the Euler Number with more decimal places Pedroski55 10 4,392 Oct-31-2021, 04:45 AM
Last Post: Pedroski55
  Random data generation sum to 1 by rounding juniorcoder 9 3,347 Oct-20-2021, 03:36 PM
Last Post: deanhystad
  Rounding issue kmll 1 1,379 Oct-08-2021, 10:35 AM
Last Post: Yoriz
  ERROR: importing desired module mbgamer28 0 1,648 Apr-05-2021, 07:46 PM
Last Post: mbgamer28
  Decimal Rounding error project_science 4 2,694 Jan-06-2021, 03:14 PM
Last Post: project_science
  Can I format decimal places by column with a dictionary? Mark17 2 2,512 Dec-28-2020, 10:13 PM
Last Post: Mark17

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020