Python十进制格式



我已经找遍了,但是找不到我问题的答案。

我需要十进制格式,其中小数可以根据情况不同。对于这种情况,我想传递一个包含十进制值的变量。

我从我的pandas DataFrame中得到的值是这种格式的3.18e-06,在这种情况下需要转换为8位小数,例如3.18123456

我可以将我的pd DF转换为基于8十进制的float64,或者我可以以某种方式将3.18e-06转换为8小数后从我的db抓取它?

最好是传递一个包含十进制格式的变量。比如:

decimal = 0.00000001
{0:.{decimal}f}".format(a)

编辑:

最后,没有一个建议的选项适合我。也许我的问题表达得不够好。我将在这里分享我的解决方案给任何可能需要它的人。

ticksize是一个变量,根据您使用的Binance交易对而变化,它的格式为:0.00001或0.0000001。

async def get_precision(ticksize):
a = '{:.{prec}f}'.format(ticksize, prec=15)
regex = "..(d+1).*"
try:
b = re.match(regex, str(a))[1]
precision = len(b)
return precision
except Exception as e:
print(f'An exception has occured on get_precision - {e}')
return False
# this function returns the length of ticksize starting from the first 0 after the dot. 
# Now that we have our precision we can use a string format to get what we need.
last_buy = '{:.{prec}f}'.format(a, prec=precision)
#returns: Last purchase price for XRPBTC: 0.00001588
float("8.99284722486562e-02")  # 0.0899284722486562

和现在的'舍入'

"{:.8f}".format(float("8.99284722486562e-02"))  # '0.08992847'

最新更新