I have a function (containing numpy calculations, float32) which returns a dictionary, where the values are lists containing text and numbers. Within the function certain numbers have been rounded to three decimal places as they are added to the dictionary. Printing the returned dictionary gives an example entry (the four rounded values are in bold):
'51450049': ['ULT', 'IRU', 71254171, '30085', '11420', '20008', '23200', '22100', '46000', 'Z1', 10.132, '---', 73566310, '50069', '10413', '20001', '23100', '22000', '46001', 'Z1', '---', -51.269, 71254171, '30085', '11420', '20008', '23200', '22100', '46000', 'Z2', 17.258, '---', 73375014, '50063', '15833', '20001', '23100', '22000', '46001', 'Z2', '---', -77.339]
later in the code, values from the returned dictionary are written to a text file using f-strings. Example code
file_object.write(f'{element_id:<13}{values[0]:<12}{values[1]:<9}{values[2]:<13}{values[3]:<9}'
f'{values[4]:<9}{values[5]:<12}{values[6]:<18}{values[7]:<9}{values[8]:<14}'
f'{values[9]:<8}{values[10]:18}{values[11]:15}\n'
)
where for instance, the {values[10]:18} command is referencing the value 10.132 in the entry above. Similar commands are used to print the other three values on different lines in the output.
However, within the outputted text file the printed values seem to have reverted back to their pre-rounded state? e.g
rounded values in dictionary:
- 10.132 -51.269 17.258 -77.339
printed by f-string
- 10.131999969482422 -51.26900100708008 17.257999420166016 -77.33899688720703
I know i can 'round' the f-string but why would i need to do this twice effectively? I was rounding the number within the function to keep the size of the returned dictionary down, but am i missing something fundamental here? i.e is the whole float16 number retained? Thanks