将科学计数法转换为浮点数 [英] Convert Scientific Notation to Float

查看:810
本文介绍了将科学计数法转换为浮点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

遇到了一个问题,我的JSON数据被打印为科学记号而不是浮点数。

Encountered a problem whereby my JSON data gets printed as a scientific notation instead of a float.

import urllib2
import json
import sys

url = 'https://bittrex.com/api/v1.1/public/getmarketsummary?market=btc-quid'
json_obj = urllib2.urlopen(url)
QUID_data = json.load(json_obj)

QUID_MarketName_Trex = QUID_data["result"][0]["MarketName"][4:9]
QUID_Last_Trex = QUID_data["result"][0]["Last"]
QUID_High_Trex = QUID_data["result"][0]["High"]
QUID_Low_Trex = QUID_data["result"][0]["Low"]
QUID_Volume_Trex = QUID_data["result"][0]["Volume"]
QUID_BaseVolume_Trex = QUID_data["result"][0]["BaseVolume"]
QUID_TimeStamp_Trex = QUID_data["result"][0]["TimeStamp"]
QUID_Bid_Trex = QUID_data["result"][0]["Bid"]
QUID_Ask_Trex = QUID_data["result"][0]["Ask"]
QUID_OpenBuyOrders_Trex = QUID_data["result"][0]["OpenBuyOrders"]
QUID_OpenSellOrders_Trex = QUID_data["result"][0]["OpenSellOrders"]
QUID_PrevDay_Trex = QUID_data["result"][0]["PrevDay"]
QUID_Created_Trex = QUID_data["result"][0]["Created"]
QUID_Change_Trex = ((QUID_Last_Trex - QUID_PrevDay_Trex)/ QUID_PrevDay_Trex)*100
QUID_Change_Var = str(QUID_Change_Trex)
QUID_Change_Final = QUID_Change_Var[0:5] + '%'

print QUID_Last_Trex   

打印以下值; 1.357e-05
我需要它是一个浮点数,其小数点后有8个字符(0.00001370)

It prints the following value; 1.357e-05. I need this to be a float with 8 chars behind the decimal (0.00001370)

如您在此处看到的-> http://i.imgur.com/FCVM1UN.jpg ,我的GUI显示正确的第一行(使用完全相同的代码) 。

As you can see here --> http://i.imgur.com/FCVM1UN.jpg, my GUI displays the first row correct (using the exact same code).

推荐答案

您正在查看默认 str()格式化浮点数,其中科学计数法用于表示足够小的数字或大数字。

You are looking at the default str() formatting of floating point numbers, where scientific notation is used for sufficiently small or large numbers.

您无需进行转换,值本身是适当的浮点数。如果您需要以其他格式显示它,请将其格式化明确

You don't need to convert this, the value itself is a proper float. If you need to display this in a different format, format it explicitly:

>>> print 0.00001357
1.357e-05
>>> print format(0.00001357, 'f')
0.000014
>>> print format(0.00001357, '.8f')
0.00001357

此处 f 格式始终对该值使用定点表示法。默认精度为6位数字; .8 指示 f 格式器显示8位数字。

Here the f format always uses fixed point notation for the value. The default precision is 6 digits; the .8 instructs the f formatter to show 8 digits instead.

默认字符串格式与 format(fpvalue,'.12g')基本相同。 g 格式根据数字的指数使用科学或定点表示形式。

The default string format is essentially the same as format(fpvalue, '.12g'); the g format uses either a scientific or fixed point presentation depending on the exponent of the number.

这篇关于将科学计数法转换为浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆