53

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 --> https://i.stack.imgur.com/qqhNc.jpg, my GUI displays the first row correct (using the exact same code).

SurvivalMachine
  • 7,946
  • 15
  • 57
  • 87
Loops
  • 581
  • 1
  • 6
  • 11

3 Answers3

83

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'.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
7

You can use print formatting:

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

Edit:

print('%.08f' % x)
Evgeny Prokurat
  • 704
  • 5
  • 8
  • Link to JSON--> https://bittrex.com/api/v1.1/public/getmarketsummary?market=btc-quid ## Object LAST reads '0.00001361' right now. Using your solution, it prints '0.000014'. It's rounded up for some reason. – Loops Aug 02 '14 at 21:43
  • yes, it is rounded to 6 numbers after the dot. as Martijn Pieters wrote, you can specify it. >>> print('%.08f' % x) – Evgeny Prokurat Aug 02 '14 at 21:52
  • btw. why you expect 0.00001370 instead of 0.00001357 for 1.357e-05? – Evgeny Prokurat Aug 02 '14 at 21:59
  • Thanks! It's an exchange rate for a cryptocurrency, those 2 last decimals make a huge difference, thus why I need all 8 to display. It works now. Thank you very much sir. – Loops Aug 02 '14 at 22:13
3

There are some approaches:


#1 float(...) + optionally round() or .format()

x = float(1.357e-05)
round(x, 6)
"{:.8f}".format(x)

#2 with decimal class

import decimal

tmp = decimal.Decimal('1.357e-05')
print('[0]', tmp)
# [0] 0.00001357

tmp = decimal.Decimal(1.357e-05)
print('[1]', tmp)
# [1] 0.0000135700000000000005188384444299032338676624931395053863525390625

decimal.getcontext().prec = 6
tmp = decimal.getcontext().create_decimal(1.357e-05)
print('[2]', tmp)
# [2] 0.0000135700

#3 with .rstrip(...)

x = ("%.17f" % n).rstrip('0').rstrip('.')

Note: there are counterparts to %f:
%f shows standard notation
%e shows scientific notation
%g shows default (scientific if 5 or more zeroes)

Roman
  • 19,236
  • 15
  • 93
  • 97