2015-01-31 66 views
1
sqrtnum_ = 0 
sqrtnum_ = cmath.sqrt(snum_) 

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

如何限制小數位數。通常我會用上面的,但它不與複數如何在使用複數時限制小數位數

+1

你就不能這樣做' 「%.12f +%.12f J」 %(sqrtnum_.real,sqrtnum_.imag)' – 2015-01-31 19:22:04

回答

1

可以使用複數的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' 
+0

燦你在小數中指定一個變量? '「%(變量)f +%.12fj」%(a.real,a.imag)' – 2015-02-02 03:38:19

+0

@MarioGalván是的,這是可能的。例如:你需要'x'小數點位置爲真,'y'爲另一個,那麼你可以做'(「%%。%df + %%。%dfj」%(x,y))%(a .real,a.imag)' – 2015-02-02 08:30:09

+0

感謝您的迴應,我也試過使用這個:'「%。* f +%.12fj」%(a.real,a.imag)',它也可以。 – 2015-02-02 15:05:37

相關問題