Skip to content

Instantly share code, notes, and snippets.

@nrobinson2000
Last active February 22, 2016 12:58
Show Gist options
  • Save nrobinson2000/c9f970f11e7a24ce3950 to your computer and use it in GitHub Desktop.
Save nrobinson2000/c9f970f11e7a24ce3950 to your computer and use it in GitHub Desktop.
bitcoin-converter.py
#!/usr/bin/env python
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("1,000,000 Bits = " + str(ask) + " USD")
print
data.close()
balance = raw_input('USD to Bits? ')
if balance != '':
balance = float(balance)
result = balance / ask * 1000000
roundedresult = int(result * 100 + 0.5) / 100.0
print(str('{:,.2f}'.format(roundedresult)) + ' Bits')
print
balance = raw_input('Bits to USD? ')
if balance != '':
balance = float(balance)
result = ask / 1000000 * 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