Why str(0.20) = 0.2 instead of 0.20 in Python? -
i trying convert float 0.20 string using
str(0.20) however, got '0.2' instead of '0.20'. know how fix this? tried search on google , stackoverflow, didn't find useful answer.
thanks in advance!
you can use python's string formatting functionality. there 3 possibilities:
f-strings, since python 3.6
value = 0.2 print(f'{value:.2f}') # 2 digits precisionstr.formatvalue = 0.2 print('{:.2f}'.format(value))% formatting
value = 0.2 print('%.2f' % value)
for more formatting details, see https://docs.python.org/3.6/library/string.html#format-string-syntax
Comments
Post a Comment