Python Forum

Full Version: need help rounding
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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
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.
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
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.
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.
(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
hanksbrad : Thank you, your solution worked! I guess I need to study up on formatting!