我正在處理具有浮動類型字段的python中的a.CSV文件。任何更好的方式來寫這個小代碼?
必須對此字段進行修改,使其具有至少4個小數點和最多8個精度小數點。
例子:
input: 5.15
output: 5.1500
input: -12.129999998
output: -12.12999999
什麼我目前做:
#The field to be modifed is present at index 3 in list temp
dotIndex = temp[3].find('.') + 1
latLen = len(temp[3])-1
if (latLen) - (dotIndex) > 8:
temp[3] = temp[3][0:dotIndex+4]
elif (latLen) - (dotIndex) < 4:
temp[3] = temp[3][0:latLen] + (4 - (latLen - (dotIndex))) * '0'
有沒有更好的方式來寫這個代碼以提高性能?
只投'臨時[3]'浮動,然後它的東西,如'打印回寫( '{:8F}'。格式())'。檢查看它有多大,拋光或切割它是太多工作。 –
你打算截斷你的價值觀,還是實際上圍繞它們?你的第二個例子,如果四捨五入適當,應該去13.13000000例如。 – asongtoruin
@asongtoruin我想截斷它們。 – noobcoder