python – 如何舍入复数?

如何将复数(例如1.9999999999999998-2j)舍入为2-2j?

当我尝试使用时

print(round(x,2))

这显示了

Traceback (most recent call last):
  File "C:\Python34\FFT.py", line 22, in <module>
    print(round(x,2))
TypeError: type complex doesn't define __round__ method

最佳答案 分别将实部和虚部分开并组合起来:

>>> num = 1.9999999999999998-2j
>>> round(num.real, 2) + round(num.imag, 2) * 1j
(2-2j)
点赞