-1

I have the following code snippet (from part of a Class):

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

Then, the following code:

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)

The problem I have is that the "Start" and "End" values are printing as long floating point numbers (e.g. 89.12999725341797).

I tried various ways to use round(), but with no success. I get errors such as:

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

and

TypeError: type LineBuffer doesn't define __round__ method

How do I round the output to two decimal places (e.g. 89.13)?

Thanks in advance!

equanimity
  • 2,371
  • 3
  • 29
  • 53
  • 1
    possibly duplicate: https://stackoverflow.com/questions/20457038/how-to-round-to-2-decimals-with-python – Frank Apr 17 '20 at 21:50
  • 1
    You've already done for `self.getcash()` what is needed: `Start: {self.data_start[0]:.2f}, End: {self.data_end[0]:.2f}` – Błotosmętek Apr 17 '20 at 21:51
  • @Frank --- I already saw https://stackoverflow.com/questions/20457038/how-to-round-to-2-decimals-with-python and did not see the solution (hence this question). Downvote? Really?? – equanimity Apr 17 '20 at 22:10
  • @Błotosmętek -- thank you! I actually tried "Start: {self.data_start[0].2f}", which threw an error message. Simple error on my part. I was missing the colon (":"). – equanimity Apr 17 '20 at 22:15
  • Does this answer your question? [How to round to 2 decimals with Python?](https://stackoverflow.com/questions/20457038/how-to-round-to-2-decimals-with-python) – Błotosmętek Apr 17 '20 at 22:36

1 Answers1

0

The solution is to append ":.2f":

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

Originally, I had appended ".2f" (missing the preceding ":"), which threw one of the error messages I saw.

Thanks @Błotosmętek!

equanimity
  • 2,371
  • 3
  • 29
  • 53