格式化复数 [英] Formatting Complex Numbers

查看:250
本文介绍了格式化复数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的一个类中的项目,我们必须输出最多五位小数的数字。输出可能是一个复数,我无法知道如何输出小数点后五位的复数。对于浮动,我知道它只是:
$ b prerintf%0.5f%variable_name



是否有类似的复数?

解决方案

对于这样的问题, Python文档应该是你的第一站。具体来说,请查看字符串格式部分。它列出了所有的字符串格式代码;复制数字不存在。



您可以使用 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.

这篇关于格式化复数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆