python - 使用复数时如何限制小数位数

标签 python formatting complex-numbers

sqrtnum_ = 0
sqrtnum_ = cmath.sqrt(snum_)

print "Using cmath.sqrt:"
print "    sqrt(", snum_, ") =      ", '%.12f' %sqrtnum_

如何限制小数位数。通常我会使用上面的内容,但它不适用于复数

最佳答案

您可以使用复数的 imagreal 属性来获取它们各自的值。

>>> a = cmath.sqrt(-1000)
>>> "%.12f + %.12fj"%(a.real,a.imag)
'0.000000000000 + 31.622776601684j'

这也适用于完全实数

>>> a = cmath.sqrt(1000)
>>> "%.12f + %.12fj"%(a.real,a.imag)
'31.622776601684 + 0.000000000000j'

关于python - 使用复数时如何限制小数位数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28255468/

相关文章:

python - 代码自动完成 'partially' 在 Spyder3 的编辑器 Pane 中工作

python - 对于set类型,为什么我们需要discard()和remove()函数?

python - 多处理池和队列

C# 字符串转日期时间错误格式输出

jquery - 如何自定义 Eclipse 的关于 jQuery 代码的格式化程序

c++ - 返回具有常量成员数的数组

import - 数学: Imported Table not same as Exported Table

python - Spyder 不运行代码中的新更改

python - 如何准备字符串以使用不明确的数组进行格式化?

python - 为什么/如何 (-1) ** 0.5 不准确?