问题描述
对于我的一个类中的项目,我们必须输出最多五位小数的数字。输出可能是一个复数,我无法知道如何输出小数点后五位的复数。对于浮动,我知道它只是:
$ b prerintf%0.5f%variable_name
是否有类似的复数?
对于这样的问题,应该是你的第一站。具体来说,请查看部分。它列出了所有的字符串格式代码;复制数字不存在。
您可以使用 x.real分别格式化数字的实部和虚部
和 x.imag
,并以 a + bi
形式打印出来。 p>
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?
For questions like this, the Python documentation should be your first stop. Specifically, have a look at the section on string formatting. It lists all the string format codes; there isn't one for complex numbers.
What you can do is format the real and imaginary parts of the number separately, using x.real
and x.imag
, and print it out in a + bi
form.
这篇关于格式化复数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!