Skip to content

Commit 4a11db5

Browse files
authored
feat: add compact= arg to for fmt_currency() (#664)
* Add compact= parameter to fmt_currency() * Add compact= arg to vals.fmt_currency() * Add test of fmt_currency(compact=True) * Refactor currency formatting based on code review
1 parent 33c7be9 commit 4a11db5

3 files changed

Lines changed: 24 additions & 5 deletions

File tree

great_tables/_formats.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,6 +1116,7 @@ def fmt_currency(
11161116
use_seps: bool = True,
11171117
accounting: bool = False,
11181118
scale_by: float = 1,
1119+
compact: bool = False,
11191120
pattern: str = "{x}",
11201121
sep_mark: str = ",",
11211122
dec_mark: str = ".",
@@ -1181,6 +1182,10 @@ def fmt_currency(
11811182
All numeric values will be multiplied by the `scale_by` value before undergoing formatting.
11821183
Since the `default` value is `1`, no values will be changed unless a different multiplier
11831184
value is supplied.
1185+
compact
1186+
Whether to use compact formatting. This is a boolean value that, when set to `True`, will
1187+
format large numbers in a more compact form (e.g., `1,000,000` becomes `1M`). This is
1188+
`False` by default.
11841189
pattern
11851190
A formatting pattern that allows for decoration of the formatted value. The formatted value
11861191
is represented by the `{x}` (which can be used multiple times, if needed) and all other
@@ -1282,6 +1287,7 @@ def fmt_currency(
12821287
use_seps=use_seps,
12831288
accounting=accounting,
12841289
scale_by=scale_by,
1290+
compact=compact,
12851291
sep_mark=sep_mark,
12861292
dec_mark=dec_mark,
12871293
force_sign=force_sign,
@@ -1302,6 +1308,7 @@ def fmt_currency_context(
13021308
use_seps: bool,
13031309
accounting: bool,
13041310
scale_by: float,
1311+
compact: bool,
13051312
sep_mark: str,
13061313
dec_mark: str,
13071314
force_sign: bool,
@@ -1326,9 +1333,14 @@ def fmt_currency_context(
13261333
if currency_symbol == "$":
13271334
currency_symbol = _context_dollar_mark(context=context)
13281335

1329-
# Format the value to decimal notation; this is done before the currency symbol is
1330-
# affixed to the value
1331-
x_formatted = _value_to_decimal_notation(
1336+
# Choose the appropriate formatting function based on the `compact=` option
1337+
if compact:
1338+
f_formatter = _format_number_compactly
1339+
else:
1340+
f_formatter = _value_to_decimal_notation
1341+
1342+
# Perform formatting to decimal notation
1343+
x_formatted = f_formatter(
13321344
value=x,
13331345
decimals=decimals,
13341346
n_sigfig=None,

great_tables/_formats_vals.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
from __future__ import annotations
22

3-
from typing import TYPE_CHECKING, Any
43
from pathlib import Path
4+
from typing import TYPE_CHECKING, Any
55

66
from typing_extensions import TypeAlias
77

8-
from .gt import GT, _get_column_of_values
98
from ._gt_data import GTData
109
from ._tbl_data import SeriesLike, to_frame
10+
from .gt import GT, _get_column_of_values
1111

1212
if TYPE_CHECKING:
1313
from ._formats import DateStyle, TimeStyle
@@ -541,6 +541,7 @@ def val_fmt_currency(
541541
accounting: bool = False,
542542
use_seps: bool = True,
543543
scale_by: float = 1,
544+
compact: bool = False,
544545
pattern: str = "{x}",
545546
sep_mark: str = ",",
546547
dec_mark: str = ".",
@@ -601,6 +602,10 @@ def val_fmt_currency(
601602
All numeric values will be multiplied by the `scale_by` value before undergoing formatting.
602603
Since the `default` value is `1`, no values will be changed unless a different multiplier
603604
value is supplied.
605+
compact
606+
Whether to use compact formatting. This is a boolean value that, when set to `True`, will
607+
format large numbers in a more compact form (e.g., `1,000,000` becomes `1M`). This is
608+
`False` by default.
604609
pattern
605610
A formatting pattern that allows for decoration of the formatted value. The formatted value
606611
is represented by the `{x}` (which can be used multiple times, if needed) and all other
@@ -652,6 +657,7 @@ def val_fmt_currency(
652657
accounting=accounting,
653658
use_seps=use_seps,
654659
scale_by=scale_by,
660+
compact=compact,
655661
pattern=pattern,
656662
sep_mark=sep_mark,
657663
dec_mark=dec_mark,

tests/test_formats.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,6 +1172,7 @@ def test_fmt_scientific_case(
11721172
(dict(placement="right"), ["1,234,567.00$", "−5,432.37$"]),
11731173
(dict(placement="right", incl_space=True), ["1,234,567.00 $", "−5,432.37 $"]),
11741174
(dict(incl_space=True), ["$ 1,234,567.00", "−$ 5,432.37"]),
1175+
(dict(compact=True), ["$1.23M", "−$5.43K"]),
11751176
]
11761177

11771178

0 commit comments

Comments
 (0)