Browse Source

kivy: format_amount: minor clean-up

patch-4
SomberNight 4 years ago
parent
commit
18c9a1af10
No known key found for this signature in database GPG Key ID: B33B5F232C6271E9
  1. 20
      electrum/gui/kivy/main_window.py
  2. 6
      electrum/gui/kivy/uix/screens.py
  3. 7
      electrum/util.py

20
electrum/gui/kivy/main_window.py

@ -24,6 +24,7 @@ from electrum import blockchain
from electrum.network import Network, TxBroadcastError, BestEffortRequestFailed
from electrum.interface import PREFERRED_NETWORK_PROTOCOL, ServerAddr
from electrum.logging import Logger
from electrum.bitcoin import COIN
from electrum.gui import messages
from .i18n import _
@ -318,7 +319,7 @@ class ElectrumWindow(App, Logger):
rate = self.fx.exchange_rate()
if rate.is_nan():
return ''
fiat_amount = self.get_amount(amount_str + ' ' + self.base_unit) * rate / pow(10, 8)
fiat_amount = self.get_amount(amount_str + ' ' + self.base_unit) * rate / COIN
return "{:.2f}".format(fiat_amount).rstrip('0').rstrip('.')
def fiat_to_btc(self, fiat_amount):
@ -327,10 +328,12 @@ class ElectrumWindow(App, Logger):
rate = self.fx.exchange_rate()
if rate.is_nan():
return ''
satoshis = int(pow(10,8) * Decimal(fiat_amount) / Decimal(rate))
satoshis = COIN * Decimal(fiat_amount) / Decimal(rate)
return format_satoshis_plain(satoshis, decimal_point=self.decimal_point())
def get_amount(self, amount_str):
def get_amount(self, amount_str: str) -> Optional[int]:
if not amount_str:
return None
a, u = amount_str.split()
assert u == self.base_unit
try:
@ -958,19 +961,16 @@ class ElectrumWindow(App, Logger):
return format_satoshis_plain(amount_after_all_fees, decimal_point=self.decimal_point())
def format_amount(self, x, is_diff=False, whitespaces=False):
return format_satoshis(
x,
num_zeros=0,
decimal_point=self.decimal_point(),
is_diff=is_diff,
whitespaces=whitespaces,
)
return self.electrum_config.format_amount(x, is_diff=is_diff, whitespaces=whitespaces)
def format_amount_and_units(self, x) -> str:
if x is None:
return 'none'
if x == '!':
return 'max'
# FIXME this is using format_satoshis_plain instead of config.format_amount
# as we sometimes convert the returned string back to numbers,
# via self.get_amount()... the need for converting back should be removed
return format_satoshis_plain(x, decimal_point=self.decimal_point()) + ' ' + self.base_unit
def format_fee_rate(self, fee_rate):

6
electrum/gui/kivy/uix/screens.py

@ -418,11 +418,7 @@ class ReceiveScreen(CScreen):
def get_URI(self):
from electrum.util import create_bip21_uri
amount = self.amount
if amount:
a, u = self.amount.split()
assert u == self.app.base_unit
amount = Decimal(a) * pow(10, self.app.decimal_point())
amount = self.app.get_amount(self.amount)
return create_bip21_uri(self.address, amount, self.message)
def do_copy(self):

7
electrum/util.py

@ -627,11 +627,16 @@ def chunks(items, size: int):
yield items[i: i + size]
def format_satoshis_plain(x, *, decimal_point=8) -> str:
def format_satoshis_plain(
x: Union[int, float, Decimal, str], # amount in satoshis,
*,
decimal_point: int = 8, # how much to shift decimal point to left (default: sat->BTC)
) -> str:
"""Display a satoshi amount scaled. Always uses a '.' as a decimal
point and has no thousands separator"""
if x == '!':
return 'max'
assert isinstance(x, (int, float, Decimal)), f"{x!r} should be a number"
scale_factor = pow(10, decimal_point)
return "{:.8f}".format(Decimal(x) / scale_factor).rstrip('0').rstrip('.')

Loading…
Cancel
Save