如果我正确理解了文档,在 python 2.6.5 中,字符串格式“{0:d}”将与使用 String.format() 格式化字符串的方式“%d”一样
" I have {0:d} dollars on me ".format(100.113)
应该打印“我身上有 100 美元”
但是我得到了错误:
ValueError: Unknown format code 'd' for object of type 'float'
其他格式操作确实有效。例如。
>>> "{0:e}".format(112121.2111)
'1.121212e+05'
最佳答案
该错误表示您将 float 传递给需要整数的格式代码。
请改用 {0:f}
。因此:
"I have {0:f} dollars on me".format(100.113)
将给予:
'I have 100.113000 dollars on me'
关于python - 字符串格式为 "{0:d}".format 为类型为 'd' 的对象提供未知格式代码 'float',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5627605/