Python 将科学记数法转换为浮点数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25099626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert Scientific Notation to Float
提问by Loops
Encountered a problem whereby my JSON data gets printed as a scientific notation instead of a float.
遇到了一个问题,我的 JSON 数据被打印为科学记数法而不是浮点数。
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
It prints the following value; 1.357e-05.
I need this to be a float with 8 chars behind the decimal (0.00001370)
它打印以下值;1.357e-05. 我需要这是一个小数点后面有 8 个字符的浮点数 (0.00001370)
As you can see here --> http://i.imgur.com/FCVM1UN.jpg, my GUI displays the first row correct (using the exact same code).
正如您在此处看到的 --> http://i.imgur.com/FCVM1UN.jpg,我的 GUI 显示正确的第一行(使用完全相同的代码)。
采纳答案by Martijn Pieters
You are looking at the default str()formattingof floating point numbers, where scientific notation is used for sufficiently small or large numbers.
您正在查看浮点数的默认str()格式,其中科学记数法用于足够小或足够大的数字。
You don't need to convert this, the value itselfis 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
Here the fformat alwaysuses fixed point notation for the value. The default precision is 6 digits; the .8instructs the fformatter to show 8 digits instead.
这里的f格式总是使用定点表示法来表示值。默认精度为 6 位;该.8命令f格式化,以显示8位数字代替。
The default string format is essentially the same as format(fpvalue, '.12g'); the gformat uses either a scientific or fixed point presentation depending on the exponent of the number.
默认字符串格式与format(fpvalue, '.12g');基本相同。该g格式根据数字的指数使用科学或定点表示。
回答by Evgeny Prokurat
You can use print formatting:
您可以使用打印格式:
x = 1.357e-05
print('%f' % x)
Edit:
编辑:
print('%.08f' % x)

