Skip to content

Instantly share code, notes, and snippets.

@nrobinson2000
Created June 15, 2016 09:07
Show Gist options
  • Save nrobinson2000/1490ef749aa98c2cb825a46b8e2f55b7 to your computer and use it in GitHub Desktop.
Save nrobinson2000/1490ef749aa98c2cb825a46b8e2f55b7 to your computer and use it in GitHub Desktop.
bitcoin-usd-converter.py
import urllib2
def turnFloat(x):
result = ""
for i in range(len(x)):
if str(x[i]).isdigit():
result = result + x[i]
if x[i] == ".":
result = result + x[i]
return float(result)
data = urllib2.urlopen("https://api.bitcoinaverage.com/ticker/global/USD/")
text = data.readlines()
ask_data = text.__getitem__(2)
ask = turnFloat(ask_data)
print("1000 mBits = " + str(ask) + " USD")
print
data.close()
balance = raw_input('USD to mBits? ')
print
if balance != '':
balance = float(balance)
result = balance / ask * 1000
roundedresult = int(result * 100 + 0.5) / 100.0
print(str('{:,.2f}'.format(roundedresult)) + ' mBits')
print
balance = raw_input('mBits to USD? ')
print
if balance != '':
balance = float(balance)
result = ask / 1000 * balance
roundedresult = int(result * 100 + 0.5) / 100.0
print(str('{:,.2f}'.format(roundedresult)) + ' USD')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment