Python3の四捨五入
Pythonの四捨五入は思った挙動をしないことがある。多分、丸め込みが原因。
In [1]: round(2.5, 0) Out[1]: 2.0 In [2]: round(2.0, 0) Out[2]: 2.0 In [3]: round(3.5, 0) Out[3]: 4.0
四捨五入はDecimalを使用したほうがいい。
from decimal import Decimal scores = '1.34 1.87 3.45 2.35 1.00 0.03 9.25'.split() def _round(a_list, y): data = list(map(Decimal, a_list)) return round(sum(data) / len(data), y) # 小数第二位の場合 print(_round(scores, 2))