Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
197 views
in Technique[技术] by (71.8m points)

python - Formatting Complex Numbers

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 how to output a complex number with five decimal places. For floats I know it is just:

print "%0.5f"%variable_name

Is there something similar for complex numbers?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could do it as is shown below using the str.format() method:

>>> n = 3.4+2.3j
>>> n
(3.4+2.3j)
>>> '({0.real:.2f} + {0.imag:.2f}i)'.format(n)
'(3.40 + 2.30i)'
>>> '({c.real:.2f} + {c.imag:.2f}i)'.format(c=n)
'(3.40 + 2.30i)'

To make it handle both positive and negative imaginary portions properly, you would need a (even more) complicated formatting operation:

>>> n = 3.4-2.3j
>>> n
(3.4-2.3j)
>>> '({0:.2f} {1} {2:.2f}i)'.format(n.real, '+-'[n.imag < 0], abs(n.imag))
'(3.40 - 2.30i)'

Update - Easier Way

Although you cannot use f as a presentation type for complex numbers using the string formatting operator %:

n1 = 3.4+2.3j
n2 = 3.4-2.3j

try:
    print('test: %.2f' % n1)
except Exception as exc:
    print('{}: {}'.format(type(exc).__name__, exc))

Output:

TypeError: float argument required, not complex

You can however use it with complex numbers via the str.format() method. This isn't explicitly documented, but is implied by the Format Specification Mini-Language documentation which just says:

'f'??Fixed point. Displays the number as a fixed-point number. The default precision is 6.

. . .so it's easy to overlook. In concrete terms, the following works in both Python 2.7.14 and 3.4.6:

print('n1: {:.2f}'.format(n1))
print('n2: {:.2f}'.format(n2))

Output:

n1: 3.10+4.20j
n2: 3.10-4.20j

This doesn't give you quite the control the code in my original answer does, but it's certainly much more concise (and handles both positive and negative imaginary parts automatically).

Update 2 - f-strings

Formatted string literals (aka f-strings) were added in Python 3.6, which means it could also be done like this in that version or later:

print(f'n1: {n1:.2f}')  # -> n1: 3.40+2.30j
print(f'n2: {n2:.3f}')  # -> n2: 3.400-2.300j

In Python 3.8.0, support for an = specifier was added to f-strings, allowing you to write:

print(f'{n1=:.2f}')  # -> n1=3.40+2.30j
print(f'{n2=:.3f}')  # -> n2=3.400-2.300j

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...