Formatting Complex Numbers

前端 未结 5 644
粉色の甜心
粉色の甜心 2020-12-10 11:30

For a project in one of my classes we have to output numbers up to five decimal places.It is possible that the output will be a complex number and I am unable to figure out

5条回答
  •  生来不讨喜
    2020-12-10 12:02

    For questions like this, the Python documentation should be your first stop. Specifically, have a look at the section on string formatting. It lists all the string format codes; there isn't one for complex numbers.

    What you can do is format the real and imaginary parts of the number separately, using x.real and x.imag, and print it out in a + bi form.

提交回复
热议问题