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 precision 
  • str.format

    value = 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

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

php - How to remove letter in front of the word laravel -

minify - Minimizing css files -