本文共 453 字,大约阅读时间需要 1 分钟。
查询持仓数据,数字货币交易所一般给出的是float
类型,且小数点十几位,为了展示便捷,只保留小数点后3位。
float
数据类型,保留小数点的方式有三种
round()
>> x = 3.897654326>> round(x, 3)3.898>> x = 3.000000>> round(x, 3)3.0
round
函数自动四舍五入;自动去掉多余的0
'%.3f'%x
>> x = 3.897654326>> '%.3f' % x3.898>> x = 3.000000>> '%.3f' % x3.000
'%.3f'%x
自动四舍五入;保留多余的0
>> from decimal import Decimal>> Decimal('3.897654326').quantize(Decimal('0.000'))3.898>> Decimal('3.000000000').quantize(Decimal('0.000'))3.000
转载地址:http://wlre.baihongyu.com/