-1

Let's say one of the answers is supposed to be 3.00, it will be printed as 3.00+0.00j.

How do I remove the j and have it as 3.00 only?

# Viete's Algorithm
def result_2(a3,a2,a0,a1):
    b = b_cof(a3,a2,a1,a0)
    a = a_cof(a3,a2,a1)
    p = P(a3, a2)
    r = -(b / 2.0)
    q = (a / 3.0)

    if ((r**2)+(q**3))<= 0.0:
        if q==0:
            theta = 0
        if q<0:
            theta = cmath.acos(r/(-q**(3.0/2.0)))

    phi1 = theta / 3.0
    phi2 = phi1 - ((2*cmath.pi) / 3.0)
    phi3 = phi1 + ((2*cmath.pi) / 3.0)

    print("X1 = ", "{:.2f}".format(2*math.sqrt(-q)*cmath.cos(phi1)-p/3.0))
    print("X2 = ", "{:.2f}".format(2*math.sqrt(-q)*cmath.cos(phi2)-p/3.0))
    print("X3 = ", "{:.2f}".format(2*math.sqrt(-q)*cmath.cos(phi3)-p/3.0))
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
soup
  • 79
  • 5

1 Answers1

4

You could drop the imaginary part from the number if it is zero:

>>> x=3+0j
>>> print(f"X1 = {x if x.imag else x.real:.2f}")
X1 = 3.00
>>> x=3+1j
>>> print(f"X1 = {x if x.imag else x.real:.2f}")
X1 = 3.00+1.00j
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251