2017-04-11 1644 views
0

Per the docs我設定的精度的位數用於打印numpy的浮動8,預計將看到這個代碼 1.12345679但沒有:如何設置在numpy float中顯示小數點後的位數?

import numpy as np 

np.set_printoptions(precision=8) 

x = np.float_(1.123456789) 

print x 
+0

http://stackoverflow.com/questions/455612/limiting-floats-to-two-decimal-points –

+0

或也許:HTTPS://docs.scipy .org/doc/numpy/reference/generated/numpy.around.html –

+1

'set_printoptions'控制如何顯示數組,這就是爲什麼它不起作用,如果將它打印爲'print(np.array([np.float_( 1.123456789)]))'。它會給你正確的結果。打印時,您最好的選擇似乎是字符串格式。 – umutto

回答

0

由於意見建議你可以使用numpy.around。

import numpy as np 

np.set_printoptions(precision=4) 

x = np.float_(1.123456789) 
print x 

x = np.around(x, 8) 
print x 

此輸出:

1.123456789 
1.12345679 
相關問題