Python 3.4.4 Limiting to 2 digits isn't working (format(self.NUMBER, '.2f'))

时间:2017-07-30 11:45:38

标签: python digits

I am trying to round a self.correct_answer = Smth. * Smth. I tried round(self.correct_answer, 2) and several different format(self.correct_answer, '.2f') type things.

Is it because of the 'self.'?

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

1 个答案:

答案 0 :(得分:0)

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

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

请记住Martijn Pieters在评论中提到的内容。 format返回一个字符串,而不是一个浮点数。