Dec-07-2019, 10:12 AM
HI,
Iam python beginner. Kindly help me. My query is, i want to round off float value to 2 decimals. if the number is 2 or 2.0 then the number to be converted into integer.
Question: lst = [2,3.96343,5.635,2.32463255,7.0,12,1.0]
Output: lst = [2,3.96,5.64,2.32,7,12,1]
Regards
Raj Kumar
Iam python beginner. Kindly help me. My query is, i want to round off float value to 2 decimals. if the number is 2 or 2.0 then the number to be converted into integer.
Question: lst = [2,3.96343,5.635,2.32463255,7.0,12,1.0]
Output: lst = [2,3.96,5.64,2.32,7,12,1]
lst=[2,3.96343,5.635,2.32463255,7.0,12,1.0]
k=[]
for i in lst:
if len(str(i))==1:
k.append(i)
elif len(str(i))==3 and str(i)[-1]==0:
k.append(int(i))
else:
k.append(round(i,2))
print(k)the above code returning the output is : [2, 3.96, 5.63, 2.32, 7.0, 12, 1.0]. But it is wrong output.Regards
Raj Kumar
