Python3.4.4限制为2位数不起作用(格式(self.NUMBER,“.2f”))

2024-04-29 03:52:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试舍入一个self.correct\u answer=Smth.*Smth公司。 我试过round(self.correct_answer, 2)和几种不同的format(self.correct_answer, '.2f')类型的东西

是因为“自我”吗

self.correct_answer = self.numbers1[self.number1] / self.numbers2[self.number2] 
format(self.correct_answer, '.2f')

Tags: answerselfformat类型公司roundcorrectnumber1
2条回答

format不在适当的位置(就像Python中任何其他与字符串相关的函数/方法一样)。您需要重新分配其返回值:

formatted = format(self.correct_answer, '.2f')

记住Martijn Pieters在评论中提到的format返回字符串,而不是浮点

相关问题 更多 >