I had some problems with unaccurate floats, so here is my solution with padding the with zeroes (python 3.10).
Terveisin, Markus
def float_to_string(myfloat:float, decimals:int=6)-> str:
""" from float to string with desired number of decimals,
if accuracy is missing use zeroes for padding
"""
strmyfloat=str(myfloat)
currentdecimals=strmyfloat[::-1].find('.')
currenttens=strmyfloat.find('.')
if currentdecimals > decimals:
return f"{round(myfloat, decimals):.{decimals}f}"
return strmyfloat.ljust(currenttens+1+decimals, '0')
if __name__ == '__main__':
print(float_to_string(myfloat=-22.12))
print(float_to_string(myfloat=22.12))
print(float_to_string(myfloat=179.1234))
print(float_to_string(myfloat=0.010))
print(float_to_string(myfloat=-0.010))
print(float_to_string(myfloat=-22.123456789))
print(float_to_string(myfloat=22.123456789))
print(float_to_string(myfloat=0.01000000))