![]() |
need help rounding - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: need help rounding (/thread-39458.html) |
need help rounding - joseph202020 - Feb-21-2023 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 RE: need help rounding - paul18fr - Feb-21-2023 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}")
RE: need help rounding - jefsummers - Feb-21-2023 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]) 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))) 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 betterdo for item in list1 and change the subsequent code, but will leave to you. RE: need help rounding - hanksbrad - Feb-21-2023 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 RE: need help rounding - snippsat - Feb-21-2023 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}")
hanksbrad Wrote:I put together a working example in Einblick. https://app.einblick.ai/?w=63f4e322f478ad93f5ab2006Have to Sign up to see the data,no one is gone do that. Do as i shown with working example code and output. RE: need help rounding - deanhystad - Feb-21-2023 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. RE: need help rounding - joseph202020 - Feb-21-2023 (Feb-21-2023, 03:29 PM)hanksbrad Wrote: U can format that like: "{:.2f}" RE: need help rounding - joseph202020 - Feb-21-2023 hanksbrad : Thank you, your solution worked! I guess I need to study up on formatting! |