Python: 如何对索引数字进行舍入?

Python: how to round an indexed number?

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

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)

我遇到的问题是 "Start" 和 "End" 值打印为长浮点数(例如 89.12999725341797)。

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

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

TypeError: type LineBuffer doesn't define __round__ method

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

提前致谢!

解决办法是追加":.2f":

self.log(f'BUY - Size: {size}, Cash: {self.getcash():.2f}, Start: {self.data_start[0]:.2f}, End: {self.data_end[0]:.2f}')

最初,我附加了 ".2f"(缺少前面的 ":"),这引发了我看到的错误消息之一。

谢谢@Błotosmętek!