格式化复数
对于我的一个课程中的一个项目,我们必须输出最多五位小数的数字。输出可能是一个复数,而我无法弄清楚如何输出具有五位小数的复数。对于浮点数,我知道它只是:
print "%0.5f"%variable_name
复数有类似的东西吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用
str.format()
方法执行如下所示的操作:为了使其正确处理正虚部和负虚部,您将需要(甚至更)复杂的格式化操作:
更新 - 更简单的方法
尽管您不能使用字符串格式化运算符
%
将f
用作复数的表示类型:输出:
但是,您可以通过以下方式将其与复数一起使用
str.format()
方法。这没有明确记录,但由 格式规范迷你语言文档只是说:。 。 .所以很容易被忽视。
具体而言,以下内容在 Python 2.7.14 和 3.4.6 中均有效:
输出:
这并不能为您提供我原来答案中的代码所提供的完全控制,但它肯定更加简洁(并且可以处理正数和负数自动虚部)。
更新 2 - f-strings
格式化字符串文字(又名f-strings)是在Python 3.6中添加的,这意味着在该版本或更高版本中也可以这样做:
在Python 3.8中。 0,支持
=
说明符 已添加到 f 字符串,允许您编写:You could do it as is shown below using the
str.format()
method:To make it handle both positive and negative imaginary portions properly, you would need a (even more) complicated formatting operation:
Update - Easier Way
Although you cannot use
f
as a presentation type for complex numbers using the string formatting operator%
:Output:
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:. . .so it's easy to overlook.
In concrete terms, the following works in both Python 2.7.14 and 3.4.6:
Output:
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:
In Python 3.8.0, support for an
=
specifier was added to f-strings, allowing you to write:字符串格式化操作 - 即模数 (
%
) 运算符) -也不是较新的
str.format()
格式字符串语法< /a> 支持复杂类型。但是,可以直接调用所有内置数字类型的
__format__
方法。这是一个示例:
请注意,这也适用于负虚部。
Neither String Formatting Operations - i.e. the modulo (
%
) operator) -nor the newer
str.format()
Format String Syntax support complex types.However it is possible to call the
__format__
method of all built in numeric types directly.Here is an example:
Note, that this works well with negative imaginary parts too.
对于此类问题,Python 文档应该是您的第一站。具体来说,请查看有关字符串格式化的部分。它列出了所有字符串格式代码;没有复数的。
您可以做的是使用
x.real
和x.imag
分别格式化数字的实部和虚部,并在a + bi 中打印出来
形式。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
andx.imag
, and print it out ina + bi
form.从 Python 2.6 开始,您可以定义自己的类的对象如何响应格式字符串。因此,您可以定义一个可以格式化的
complex
子类。下面是一个示例:此类对象的行为与复数完全相同,只是它们占用更多空间且运行速度更慢;读者要小心。
As of Python 2.6 you can define how objects of your own classes respond to format strings. So, you can define a subclass of
complex
that can be formatted. Here's an example:Objects of this class behave exactly like
complex
numbers except they take more space and operate more slowly; reader beware.看看这个:
我已经成功打印了我的complexfloat的表达式:
out before:
out after:
Check this out:
I've successfully printed my complexfloat's expressions:
out before:
out after: