Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
need help rounding
#3
Not sure why round is not working for you. I made up some data for list1 and list2 and ratio. Your version as posted:
list1 = [2.2,6.1,7.7,10]
list2 = [12.345,23.456,34.567,5]
ratio = 3.1415926535
items = len(list1)
for n in range (0,items,1):
    print("\t\t\t",(list1[n]),end = "\t\t\t\t")
    print((float(list1[n]) * ratio),("\t"),list2[n]) 
Output:
2.2 6.911503837700001 12.345 6.1 19.16371518635 23.456 7.7 24.19026343195 34.567 10 31.415926535 5
Now, properly put round
list1 = [2.2,6.1,7.7,10]
list2 = [12.345,23.456,34.567,5]
ratio = 3.1415926535
items = len(list1)
for n in range (0,items,1):
    print("\t\t\t",round(list1[n],2),end = "\t\t\t\t")
    print(str(round(float(list1[n]) * ratio,2))+"\t"+str(+round(list2[n],2)))
Output:
2.2 6.91 12.35 6.1 19.16 23.46 7.7 24.19 34.57 10 31.42 5
There are a number of other issues in your code snippet - the range operator for one could be dropped back to range(items), or even better
do for item in list1 and change the subsequent code, but will leave to you.
Reply


Messages In This Thread
need help rounding - by joseph202020 - Feb-21-2023, 06:23 AM
RE: need help rounding - by paul18fr - Feb-21-2023, 07:16 AM
RE: need help rounding - by jefsummers - Feb-21-2023, 01:04 PM
RE: need help rounding - by hanksbrad - Feb-21-2023, 03:29 PM
RE: need help rounding - by joseph202020 - Feb-21-2023, 08:11 PM
RE: need help rounding - by snippsat - Feb-21-2023, 04:17 PM
RE: need help rounding - by deanhystad - Feb-21-2023, 04:38 PM
RE: need help rounding - by joseph202020 - Feb-21-2023, 08:13 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  from numpy array to csv - rounding SchroedingersLion 6 2,410 Nov-14-2022, 09:09 PM
Last Post: deanhystad
  Random data generation sum to 1 by rounding juniorcoder 9 3,636 Oct-20-2021, 03:36 PM
Last Post: deanhystad
  Rounding issue kmll 1 1,491 Oct-08-2021, 10:35 AM
Last Post: Yoriz
  Not rounding to desired decimal places? pprod 2 2,637 Mar-05-2021, 11:11 AM
Last Post: pprod
  Decimal Rounding error project_science 4 2,860 Jan-06-2021, 03:14 PM
Last Post: project_science
  rounding and floats Than999 2 3,230 Oct-26-2020, 09:36 PM
Last Post: deanhystad
  Rounding to the nearest eight wallgraffiti 2 2,174 Jul-15-2020, 06:05 PM
Last Post: wallgraffiti
  rounding question DPaul 16 5,887 Apr-12-2020, 02:30 PM
Last Post: DPaul
  price + tax rounding mlieqo 11 6,689 Sep-21-2019, 04:53 PM
Last Post: mlieqo
  rounding floats to a number of bits Skaperen 2 2,387 Sep-13-2019, 04:37 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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