Skip to content

Commit a2ddd46

Browse files
committed
Fix formatting small numb ers in crypto plugin
1 parent 7fc8abc commit a2ddd46

File tree

2 files changed

+44
-13
lines changed

2 files changed

+44
-13
lines changed

plugins/cryptocurrency.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import inspect
1313
import time
1414
import warnings
15+
from decimal import Decimal
1516
from numbers import Real
1617
from operator import itemgetter
1718
from threading import RLock
@@ -628,9 +629,9 @@ def crypto_command(text, event):
628629
quote = data.quote[currency]
629630
change = cast(Union[int, float], quote.percent_change_24h)
630631
if change > 0:
631-
change_str = "$(dark_green)+{}%$(clear)".format(change)
632+
change_str = colors.parse("$(dark_green)+{}%$(clear)").format(change)
632633
elif change < 0:
633-
change_str = "$(dark_red){}%$(clear)".format(change)
634+
change_str = colors.parse("$(dark_red){}%$(clear)").format(change)
634635
else:
635636
change_str = "{}%".format(change)
636637

@@ -642,20 +643,31 @@ def crypto_command(text, event):
642643
else:
643644
btc = ""
644645

646+
num_format = format_price(quote.price)
647+
645648
return colors.parse(
646-
(
647-
"{} ({}) // $(orange){}{:,.2f}$(clear) {} " + btc + "// {} change"
648-
).format(
649-
data.symbol,
650-
data.slug,
651-
currency_sign,
652-
quote.price,
653-
currency,
654-
change_str,
655-
)
649+
"{} ({}) // $(orange){}{}$(clear) {} " + btc + "// {} change"
650+
).format(
651+
data.symbol,
652+
data.slug,
653+
currency_sign,
654+
num_format,
655+
currency,
656+
change_str,
656657
)
657658

658659

660+
def format_price(price: Union[int, float, Real]) -> str:
661+
price = float(price)
662+
if price < 1:
663+
precision = max(2, min(10, -Decimal(str(price)).as_tuple().exponent))
664+
num_format = "{:01,.{}f}".format(price, precision)
665+
else:
666+
num_format = "{:,.2f}".format(price)
667+
668+
return num_format
669+
670+
659671
@hook.command("currencies", "currencylist", autohelp=False)
660672
def currency_list():
661673
"""- List all available currencies from the API"""

tests/plugin_tests/test_cryptocurrency.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ def init_response(
5858
check_api_key=False,
5959
pct_change=18.9,
6060
show_btc=True,
61+
price=50000000000.0,
6162
):
6263
if check_api_key:
6364
cryptocurrency.init_api(bot.get())
@@ -134,7 +135,7 @@ def init_response(
134135
"tags": [],
135136
"quote": {
136137
"USD": {
137-
"price": 50000000000,
138+
"price": price,
138139
"volume_24h": 20,
139140
"market_cap": 92,
140141
"percent_change_1h": 14.5,
@@ -279,6 +280,24 @@ def test_cache(freeze_time):
279280
assert c.get("foo") is None
280281

281282

283+
@pytest.mark.parametrize(
284+
"price,out",
285+
[
286+
(1, "1.00"),
287+
(50000, "50,000.00"),
288+
(10.2548, "10.25"),
289+
(0.1, "0.10"),
290+
(0.0241, "0.0241"),
291+
(0.00241, "0.00241"),
292+
(0.000241, "0.000241"),
293+
(0.0000241, "0.0000241"),
294+
(0.001231549654135151564, "0.0012315497"),
295+
],
296+
)
297+
def test_format_price(price, out):
298+
assert cryptocurrency.format_price(price) == out
299+
300+
282301
def test_crypto_cmd(mock_requests):
283302
init_response(mock_requests)
284303

0 commit comments

Comments
 (0)