0

I am trying to write some floats numbers as decimals with 14 digit after floating point. So i want to add traling zeros for numbers who have less than 14 digits . however, when I use the folowing, it performs a random approximation rather than adding zeros .

print ('%.14f' % 381.949875)
print ('%.14f' % 383.558625 )
print ( '%.14f' % 382.593375)

The result is as follows :

381.94987500000002
383.55862500000001
382.59337499999998

Any clue ?

Lyesgigs
  • 119
  • 1
  • 12
  • It is definitely something related to https://stackoverflow.com/questions/588004/is-floating-point-math-broken as `381.949875 + 0.00000000000001 == 381.949875`. I would have to check in more detail but I suspect that in order to do the print, behind the curtains the first number is added to a very small 15 digit after the decimal number in order to ensure the 0 padding. – JonSG Feb 03 '22 at 14:40

1 Answers1

0

You are looking for this formatting I believe

f"{381.949875:0<14}"
f"{383.558625:0<14}"
f"{382.593375:0<14}"
Mathias Sven
  • 349
  • 3
  • 7