I'd like to calculate up to 20 decimal places. the answer I want 0.60000000000000000000 My answer 0.59999999999999997780 How do I solve this? my code
a,b=map(float,input().split())
print("%.20f"%(a/b))
I'd like to calculate up to 20 decimal places. the answer I want 0.60000000000000000000 My answer 0.59999999999999997780 How do I solve this? my code
a,b=map(float,input().split())
print("%.20f"%(a/b))
print('{:<020}'.format(0.6));
use format , and right padding
print('{:<020}'.format(a/b));
So for your case you can use :
from decimal import *
getcontext().prec = 20
result = ("{:.20f}".format(Decimal("0.6")))
print(result)
or
result=5/4 #add a/b here
formatedResult = ('{:<0'+str((len(str(float(result)).split('.')[0])+21))+'}').format(float(result))
print("The formated result: " + formatedResult)
print("Length of digit after decimal point: "+str(len(formatedResult.split('.')[1])))
Here we find number of characters before the decimal point and add 21 to it (including the decimal character)