【发布时间】:2022-01-19 07:35:14
【问题描述】:
我尝试读取一个 json 文件 输出给出奇怪的浮点数
250:4.918e-06
251: 0.0006678
252: 4.366e-07
253: 3.0054e-06
254: 3.0942e-05
我做错了什么?
url= 'https://cryptobubbles.net/backend/data/currentBubbles1000.json'
user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7'
headers={'User-Agent':user_agent,}
req= urllib.request.Request(url,None,headers)
teller = 0
def get_data_binance():
global teller
json_content = json.loads(urlopen(req).read())
for coins in json_content:
if coins['binanceSymbol'] and coins['data']['btc']['price']:
teller += 1
zeroteller = str(teller)
zero_filled_number = zeroteller.zfill(3)
binancefilled = coins['binanceSymbol']
#print(zero_filled_number+ ': '+binancefilled.ljust(12)+ ' '+str(coins['data']['usd']['price']))
print (zero_filled_number+ ': '+str(coins['data']['btc']['price']))
【问题讨论】:
-
没有错,它只是浮动的另一种表示,工作方式相同。只需确保在打印时正确格式化即可。
-
如果您需要准确性,您应该改用Decimals。