0

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))
박태규
  • 27
  • 4

1 Answers1

0
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)

PDHide
  • 18,113
  • 2
  • 31
  • 46
  • @superbrain please try that !!!!!!!!!!! rounding the answer will become 6 and then using %.20f for 6 prints 6 with 20 zeros – PDHide Jan 17 '21 at 19:33
  • @superbrain did you downvote ? If someone else downvoted please leave a comment and explain why so it would helpful for others – PDHide Jan 17 '21 at 19:34
  • Looks better, though it's missing two zeros. And for 12.6 it's missing *three* zeros. And for 1/7 it ends with a 0 instead of a 7 (granted, that's partly the fault of the inaccurate calculation, but the OP does talk about the calculation, not just the printing, so they might want the correct digit there). – superb rain Jan 18 '21 at 00:17
  • @superbrain updated the answer thanks for pointting that out see the updated answer – PDHide Jan 18 '21 at 09:50