我希望在昏迷後總是有5個數字。在Python中將輸出格式化爲固定精度
示例: 我的數據是像105.56565 105 165.54 ......
我想是這樣的相同長度:105.56565 105.00000 165.54000 ....
腳本:
from pylab import*
from rtlsdr import*
from bluetooth import*
import sys
#configure device
sdr= RtlSdr()
sdr.sample_rate=double(sys.argv[3])
sdr.gain=double(sys.argv[2])
sdr.center_freq=double(sys.argv[1])
#Bluetooth connection
server_sock=BluetoothSocket(RFCOMM)
server_sock.bind(("",PORT_ANY))
server_sock.listen(1)
port=server_sock.getsockname()[1]
uuid="94f39d29-7d6d-437d-973b-fba39e49d4ee"
client_sock,client_info=server_sock.accept()
while(1):
samples=sdr.read_samples(256*1024)
result=psd(samples,NFFT=1024, Fs=sdr.sample_rate/1e6, Fc=sdr.center_freq*1e6/1e6)
freq=result[1]/1e6
value_freq=str(freq)[1:-1]
print format(freq, '%5f') // he do not work
這會截斷數字,不會將其舍入到所需的小數位數。試試'n = 105.5656599999' – ktdrv