Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
need help rounding
#1
How do I round the float in last line to 2 places = x.xx instead of x.xxxxxxxxxx
it won't do it most of the time but it will with certain entered combinations.
round(n,2), or other variations did not work.

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]) line 6 float to 2 places in (floatlis[n]*ratio) I need help here

thanks for any help, Joseph
Reply
#2
Not clear for me, but the following should help you i guess (especially n4):

N = 3.14159
n1 = round(N, 3)
n2 = f'{N:.3f}'
n3 = str(N).split('.')
n4 = float(n3[0] + '.' + n3[1][:3])
print(f"N = {N} - n1 = {n1} - n2 = {n2} - n3 = {n3} - n4 = {n4}")
Output:
N = 3.14159 - n1 = 3.142 - n2 = 3.142 - n3 = ['3', '14159'] - n4 = 3.141
Reply
#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
#4
U can format that like: "{:.2f}"

for n in range(0, items, 1):
     print("\t\t\t", list1[n], end="\t\t\t\t")
     print("{:.2f}\t{}".format(float(list1[n]) * ratio, list2[n]))
the "{:.2f}" format string to specifies that we want to format the float to two decimal places. The f in "{:.2f}" stands for "float", and the .2 specifies that we want two decimal places. The {:} syntax within the curly braces is used to denote the value to be formatted, which results from float(list1[n]) * ratio.


I put together a working example in Einblick. https://app.einblick.ai/?w=63f4e322f478ad93f5ab2006
joseph202020 likes this post
Reply
#5
Some bad looping here,no range() or len() needed just loop over the list.
Now should also use f-string.
list1 = [2.2, 6.1, 7.7, 10]
ratio = 3.1415926535
for n in list1:
    print(f"{n * ratio:.2f}\t{n}")
Output:
6.91 2.2 19.16 6.1 24.19 7.7 31.42 10
hanksbrad Wrote:I put together a working example in Einblick. https://app.einblick.ai/?w=63f4e322f478ad93f5ab2006
Have to Sign up to see the data,no one is gone do that.
Do as i shown with working example code and output.
Reply
#6
Do you want to round numbers to some precision, or do you want to format output for printing? These are two different things, and are accomplished using different tools. If all you want is to print floats with two decimal places, use one of Python's many formatters (use f'string). If you want to round the numerical value of a float to some precision, use round. Be aware that floats do not have infinite precision and rounding may not give you the exact value you want.
Reply
#7
(Feb-21-2023, 03:29 PM)hanksbrad Wrote: U can format that like: "{:.2f}"

for n in range(0, items, 1):
     print("\t\t\t", list1[n], end="\t\t\t\t")
     print("{:.2f}\t{}".format(float(list1[n]) * ratio, list2[n]))
the "{:.2f}" format string to specifies that we want to format the float to two decimal places. The f in "{:.2f}" stands for "float", and the .2 specifies that we want two decimal places. The {:} syntax within the curly braces is used to denote the value to be formatted, which results from float(list1[n]) * ratio.


I put together a working example in Einblick. https://app.einblick.ai/?w=63f4e322f478ad93f5ab2006
Reply
#8
hanksbrad : Thank you, your solution worked! I guess I need to study up on formatting!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  from numpy array to csv - rounding SchroedingersLion 6 2,205 Nov-14-2022, 09:09 PM
Last Post: deanhystad
  Random data generation sum to 1 by rounding juniorcoder 9 3,455 Oct-20-2021, 03:36 PM
Last Post: deanhystad
  Rounding issue kmll 1 1,424 Oct-08-2021, 10:35 AM
Last Post: Yoriz
  Not rounding to desired decimal places? pprod 2 2,574 Mar-05-2021, 11:11 AM
Last Post: pprod
  Decimal Rounding error project_science 4 2,774 Jan-06-2021, 03:14 PM
Last Post: project_science
  rounding and floats Than999 2 3,126 Oct-26-2020, 09:36 PM
Last Post: deanhystad
  Rounding to the nearest eight wallgraffiti 2 2,096 Jul-15-2020, 06:05 PM
Last Post: wallgraffiti
  rounding question DPaul 16 5,645 Apr-12-2020, 02:30 PM
Last Post: DPaul
  price + tax rounding mlieqo 11 6,473 Sep-21-2019, 04:53 PM
Last Post: mlieqo
  rounding floats to a number of bits Skaperen 2 2,323 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