float
查询持仓数据,数字货币交易所一般给出的是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' % x
3.898
>> x = 3.000000
>> '%.3f' % x
3.000
'%.3f'%x
自动四舍五入;保留多余的0
三、decimal
>> from decimal import Decimal
>> Decimal('3.897654326').quantize(Decimal('0.000'))
3.898
>> Decimal('3.000000000').quantize(Decimal('0.000'))
3.000
References
- python里如何保存float类型的小数的位数