How can I round up a complex number (e.g. 1.9999999999999998-2j
) as 2-2j
?
When I tried using
print(round(x,2))
it showed
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
Round real part and imaginary part separately and combine them:
If all you want to do is represent the value rounded as shown, rather than modify the value itself, the following works:
See: Format Specification Mini-Language.
Id say the best way to do it is as such
if you don't want to repeat that every time you want to do it, you could put it in a function.
Additional optional arguments can then be added to this, so if you only want to round one part for example, or if you only want to round to a certain number of decimal places on either the real or complex part
as the variables are auto set to true or 0, you don't need to input them unless you specifically want too. But they are handy to have