一尘不染

如何舍入一个复数?

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

阅读 231

收藏
2021-01-20

共1个答案

一尘不染

将实部和虚部分别取整,然后将它们组合:

>>> num = 1.9999999999999998-2j
>>> round(num.real, 2) + round(num.imag, 2) * 1j
(2-2j)
2021-01-20