Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Scientific Notation to Float

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   

It prints the following value; 1.357e-05. I need this to be a float with 8 chars behind the decimal (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).

like image 684
Loops Avatar asked Aug 02 '14 21:08

Loops


People also ask

How do you convert from float to scientific notation?

To convert a floating decimal point number to scientific notation you need to multiple or divide the pre-exponential by 10 to a power, until there is just one digit to the left of the decimal point.

How do you convert float to scientific notation in Python?

You are looking at the default str() formatting of floating point numbers, where scientific notation is used for sufficiently small or large numbers. 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.

How do you convert from scientific notation to standard notation?

When converting from standard form to scientific notation, start by placing the decimal point after the first significant digit. For example, in the number 54,000,000,000 the decimal would be placed between the 5 and the 4. Now multiply this (5.4) by a power of ten.


2 Answers

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

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.

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

like image 175
Martijn Pieters Avatar answered Oct 18 '22 17:10

Martijn Pieters


You can use print formatting:

x = 1.357e-05    
print('%f' % x)

Edit:

print('%.08f' % x)
like image 25
Evgeny Prokurat Avatar answered Oct 18 '22 16:10

Evgeny Prokurat