Python:如何舍入索引的数字?

问题描述 投票:-1回答:1

我有以下代码段(来自类的一部分):

def __init__(self):
    self.data_end = self.datas[0].end
    self.data_start = self.datas[0].start

然后输入以下代码:

def next_trans(self):
    if not self.position:
        if self.buy_signal > 0:
            size = int(self.getcash() / self.datas[0].start)
            self.log(f'BUY - Size: {size}, Cash: {self.getcash():.2f}, Start: {self.data_start[0]}, End: {self.data_end[0]}')
            self.start(size=size)

我的问题是“开始”和“结束”值正在打印为长浮点数(例如89.12999725341797。)>

我尝试了多种方法来使用round(),但没有成功。我收到诸如以下错误:

AttributeError: 'LineBuffer' object has no attribute 'round'

TypeError: type LineBuffer doesn't define __round__ method

如何将输出舍入到小数点后两位(例如89.13)?

提前感谢!

我有以下代码段(来自类的一部分):def __init __(self):self.data_end = self.datas [0] .end self.data_start = self.datas [0] .start然后,执行以下操作代码:def ...

python rounding
1个回答
0
投票

解决方案是附加":.2f"

© www.soinside.com 2019 - 2024. All rights reserved.