tuse decimal instead of float - electrum - Electrum Bitcoin wallet HTML git clone https://git.parazyd.org/electrum DIR Log DIR Files DIR Refs DIR Submodules --- DIR commit 29ff2e21340ec2922485732efb113efa1baeb25a DIR parent 9c00bdd1ac6d8b1c119d0f8a7866d9d7e726aa38 HTML Author: ThomasV <thomasv@gitorious> Date: Tue, 6 Dec 2011 21:05:46 +0100 use decimal instead of float Diffstat: M client/electrum.py | 13 +++++++------ M client/gui.py | 15 +++++++++------ 2 files changed, 16 insertions(+), 12 deletions(-) --- DIR diff --git a/client/electrum.py b/client/electrum.py t@@ -18,6 +18,7 @@ import sys, base64, os, re, hashlib, socket, getpass, copy, operator, ast +from decimal import Decimal try: import ecdsa t@@ -383,7 +384,9 @@ class Wallet: self.change_addresses, self.status, self.history, self.labels, self.addressbook) = sequence except: - raise BaseException("version error.") + # it is safer to exit immediately + print "Error; could not parse wallet." + exit(1) if self.version != WALLET_VERSION: raise BaseException("Wallet version error.\nPlease move your balance to a new wallet.\nSee the release notes for more informations.") self.update_tx_history() t@@ -490,8 +493,6 @@ class Wallet: def choose_inputs_outputs( self, to_addr, amount, fee, password): """ todo: minimize tx size """ - amount = int( 1e8*amount ) - fee = int( 1e8*fee ) total = 0 inputs = [] for addr in self.addresses: t@@ -609,7 +610,7 @@ class Wallet: def mktx(self, to_address, amount, label, password, fee=None): if not self.is_valid(to_address): return False, "Invalid address" - if fee is None: fee = self.fee + if fee is None: fee = int( self.fee ) try: inputs, outputs = wallet.choose_inputs_outputs( to_address, amount, fee, password ) if not inputs: t@@ -720,9 +721,9 @@ if __name__ == '__main__': if cmd in ['payto', 'mktx']: try: to_address = args[1] - amount = float(args[2]) + amount = int( 100000000 * Decimal(args[2]) ) label = ' '.join(args[3:]) - if options.tx_fee: options.tx_fee = float(options.tx_fee) + if options.tx_fee: options.tx_fee = int( 100000000 * Decimal(options.tx_fee) ) except: firstarg = cmd cmd = 'help' DIR diff --git a/client/gui.py b/client/gui.py t@@ -23,14 +23,17 @@ import pygtk pygtk.require('2.0') import gtk, gobject import pyqrnative +from decimal import Decimal gtk.gdk.threads_init() APP_NAME = "Electrum" def format_satoshis(x): - xx = ("%f"%(x*1e-8)).rstrip('0') - if xx[-1] =='.': xx+="00" - if xx[-2] =='.': xx+="0" + xx = str( Decimal(x) /100000000 ) + #xx = ("%f"%(x*1e-8)).rstrip('0') + if not '.' in xx: xx = xx + '.' + if len(xx) > 0 and xx[-1] =='.': xx+="00" + if len(xx) > 1 and xx[-2] =='.': xx+="0" return xx def numbify(entry, is_int = False): t@@ -201,7 +204,7 @@ def run_settings_dialog(wallet, is_create, is_recovery, parent): fee_label.set_size_request(150,10) fee_label.show() fee.pack_start(fee_label,False, False, 10) - fee_entry.set_text("%f"%(wallet.fee)) + fee_entry.set_text( str( Decimal(wallet.fee) /100000000 ) ) fee_entry.connect('changed', numbify, False) fee_entry.show() fee.pack_start(fee_entry,False,False, 10) t@@ -243,7 +246,7 @@ def run_settings_dialog(wallet, is_create, is_recovery, parent): if is_recovery: gap = int(gap) if not is_create: - fee = float(fee) + fee = int( 100000000 * Decimal(fee) ) gap = int(gap) except: show_message("error") t@@ -582,7 +585,7 @@ class BitcoinGUI: return try: - amount = float(amount_entry.get_text()) + amount = int( Decimal(amount_entry.get_text()) * 100000000 ) except: show_message( "invalid amount" ) return