[转载]python浮点计算
round返回一个浮点数,可以在第二个参数控制精度
round返回一个浮点数,可以在第二个参数控制精度
>>> round(0.254343434,2)
0.25
>>> round(0.254343434,3)
0.254
python中的浮点计算可以用decimal模块
>>>from decimal import *
>>>print Decimal(1) / Decimal(3)
0.3333333333333333333333333333
>>>getcontext().prec = 5 #调整精度
>>>print Decimal(1) / Decimal(3)
0.33333
>>> round(0.254343434,2)
0.25
>>> round(0.254343434,3)
0.254
python中的浮点计算可以用decimal模块
>>>from decimal import *
>>>print Decimal(1) / Decimal(3)
0.3333333333333333333333333333
>>>getcontext().prec = 5 #调整精度
>>>print Decimal(1) / Decimal(3)
0.33333
Life is a journey, not a destination.
浙公网安备 33010602011771号