Unit converter

iFAiFA Posts: 7Member
edited September 2016 in Python Implementation
**edit**
I have modified my code. This is now much better.
**edit**

I have coded Ethereum unit converter for python without any libraries. Supports the most used units: wei, szabo, finney, ether
I would like to share it for everyone, feel free to use:
def convertEthereumUnit(value, fromWei=False, fromSzabo=False, fromFinney=False, fromEther=False, toWei=False, toSzabo=False, toFinney=False, toEther=False, prec=-1):
def moveDot(value, prec, pos):
value = value.zfill(19)
dot = value.find('.')
if dot == -1: dot = len(value)
value = value.replace('.','') + '0'*19
value = (value[:dot+pos] + '.' + value[dot+pos:]).strip('0')
if value[0] == '.': value = '0'+value
if prec == 0:
value = value[:value.find('.')]
elif prec > 0:
value += '0'*18
value = value[:value.find('.')+prec+1]
else:
if value[-1] == '.': value = value[:-1]
return value
if not type(value) is str: value=str(value)
value=value.replace(',','.')
if value.count('.') > 1: raise ValueError
if fromWei:
if toSzabo: return moveDot(value, prec, -12)
elif toFinney: return moveDot(value, prec, -15)
elif toEther: return moveDot(value, prec, -18)
elif fromSzabo:
if toWei: return moveDot(value, prec, 12)
elif toFinney: return moveDot(value, prec, -3)
elif toEther: return moveDot(value, prec, -6)
elif fromFinney:
if toWei: return moveDot(value, prec, 15)
elif toSzabo: return moveDot(value, prec, 3)
elif toEther: return moveDot(value, prec, -3)
elif fromEther:
if toWei: return moveDot(value, prec, 18)
elif toSzabo: return moveDot(value, prec, 6)
elif toFinney: return moveDot(value, prec, 3)
return value


Example for use:
>>> convertEthereumUnit('1234567890123456789', fromWei=True, toEther=True)
'1.234567890123456789'

>>> convertEthereumUnit('1234567890123456789', fromWei=True, toEther=True, prec=2)
'1.23'


Some assert check:
assert convertEthereumUnit('1000', fromEther=True, toWei=True) == '1000000000000000000000'
assert convertEthereumUnit('1000', fromEther=True, toSzabo=True) == '1000000000'
assert convertEthereumUnit('1000', fromEther=True, toFinney=True) == '1000000'
assert convertEthereumUnit('0.000000000000000001', fromEther=True, toWei=True) == '1'
assert convertEthereumUnit('10000000.000000000000000001', fromEther=True, toWei=True) == '10000000000000000000000001'
assert convertEthereumUnit('0.000001', fromEther=True, toSzabo=True) == '1'
assert convertEthereumUnit('0.001', fromEther=True, toFinney=True) == '1'
assert convertEthereumUnit('1000', fromFinney=True, toWei=True) == '1000000000000000000'
assert convertEthereumUnit('1000', fromFinney=True, toSzabo=True) == '1000000'
assert convertEthereumUnit('1000', fromFinney=True, toEther=True) == '1'
assert convertEthereumUnit('0.000000000000001', fromFinney=True, toWei=True) == '1'
assert convertEthereumUnit('10000000.000000000000001', fromFinney=True, toWei=True) == '10000000000000000000001'
assert convertEthereumUnit('0.001', fromFinney=True, toSzabo=True) == '1'
assert convertEthereumUnit('0.1', fromFinney=True, toEther=True) == '0.0001'
assert convertEthereumUnit('1000', fromSzabo=True, toWei=True) == '1000000000000000'
assert convertEthereumUnit('1000', fromSzabo=True, toFinney=True) == '1'
assert convertEthereumUnit('1000', fromSzabo=True, toEther=True) == '0.001'
assert convertEthereumUnit('0.000000000001', fromSzabo=True, toWei=True) == '1'
assert convertEthereumUnit('10000000.000000000001', fromSzabo=True, toWei=True) == '10000000000000000001'
assert convertEthereumUnit('0.000000000001', fromSzabo=True, toFinney=True) == '0.000000000000001'
assert convertEthereumUnit('0.000000000001', fromSzabo=True, toEther=True) == '0.000000000000000001'
assert convertEthereumUnit('1', fromWei=True, toSzabo=True) == '0.000000000001'
assert convertEthereumUnit('1', fromWei=True, toFinney=True) == '0.000000000000001'
assert convertEthereumUnit('1', fromWei=True, toEther=True) == '0.000000000000000001'
assert convertEthereumUnit('1000', fromWei=True, toSzabo=True) == '0.000000001'
assert convertEthereumUnit('1000', fromWei=True, toFinney=True) == '0.000000000001'
assert convertEthereumUnit('1000', fromWei=True, toEther=True) == '0.000000000000001'
Post edited by iFA on
Sign In or Register to comment.