Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions great_tables/_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -1116,6 +1116,7 @@ def fmt_currency(
use_seps: bool = True,
accounting: bool = False,
scale_by: float = 1,
compact: bool = False,
pattern: str = "{x}",
sep_mark: str = ",",
dec_mark: str = ".",
Expand Down Expand Up @@ -1181,6 +1182,10 @@ def fmt_currency(
All numeric values will be multiplied by the `scale_by` value before undergoing formatting.
Since the `default` value is `1`, no values will be changed unless a different multiplier
value is supplied.
compact
Whether to use compact formatting. This is a boolean value that, when set to `True`, will
format large numbers in a more compact form (e.g., `1,000,000` becomes `1M`). This is
`False` by default.
pattern
A formatting pattern that allows for decoration of the formatted value. The formatted value
is represented by the `{x}` (which can be used multiple times, if needed) and all other
Expand Down Expand Up @@ -1282,6 +1287,7 @@ def fmt_currency(
use_seps=use_seps,
accounting=accounting,
scale_by=scale_by,
compact=compact,
sep_mark=sep_mark,
dec_mark=dec_mark,
force_sign=force_sign,
Expand All @@ -1302,6 +1308,7 @@ def fmt_currency_context(
use_seps: bool,
accounting: bool,
scale_by: float,
compact: bool,
sep_mark: str,
dec_mark: str,
force_sign: bool,
Expand All @@ -1326,9 +1333,14 @@ def fmt_currency_context(
if currency_symbol == "$":
currency_symbol = _context_dollar_mark(context=context)

# Format the value to decimal notation; this is done before the currency symbol is
# affixed to the value
x_formatted = _value_to_decimal_notation(
# Choose the appropriate formatting function based on the `compact=` option
if compact:
Comment thread
rich-iannone marked this conversation as resolved.
f_formatter = _format_number_compactly
else:
f_formatter = _value_to_decimal_notation

# Perform formatting to decimal notation
x_formatted = f_formatter(
value=x,
decimals=decimals,
n_sigfig=None,
Expand Down
10 changes: 8 additions & 2 deletions great_tables/_formats_vals.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Any
from pathlib import Path
from typing import TYPE_CHECKING, Any

from typing_extensions import TypeAlias

from .gt import GT, _get_column_of_values
from ._gt_data import GTData
from ._tbl_data import SeriesLike, to_frame
from .gt import GT, _get_column_of_values

if TYPE_CHECKING:
from ._formats import DateStyle, TimeStyle
Expand Down Expand Up @@ -541,6 +541,7 @@ def val_fmt_currency(
accounting: bool = False,
use_seps: bool = True,
scale_by: float = 1,
compact: bool = False,
pattern: str = "{x}",
sep_mark: str = ",",
dec_mark: str = ".",
Expand Down Expand Up @@ -601,6 +602,10 @@ def val_fmt_currency(
All numeric values will be multiplied by the `scale_by` value before undergoing formatting.
Since the `default` value is `1`, no values will be changed unless a different multiplier
value is supplied.
compact
Whether to use compact formatting. This is a boolean value that, when set to `True`, will
format large numbers in a more compact form (e.g., `1,000,000` becomes `1M`). This is
`False` by default.
pattern
A formatting pattern that allows for decoration of the formatted value. The formatted value
is represented by the `{x}` (which can be used multiple times, if needed) and all other
Expand Down Expand Up @@ -652,6 +657,7 @@ def val_fmt_currency(
accounting=accounting,
use_seps=use_seps,
scale_by=scale_by,
compact=compact,
pattern=pattern,
sep_mark=sep_mark,
dec_mark=dec_mark,
Expand Down
1 change: 1 addition & 0 deletions tests/test_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -1172,6 +1172,7 @@ def test_fmt_scientific_case(
(dict(placement="right"), ["1,234,567.00$", "−5,432.37$"]),
(dict(placement="right", incl_space=True), ["1,234,567.00 $", "−5,432.37 $"]),
(dict(incl_space=True), ["$ 1,234,567.00", "−$ 5,432.37"]),
(dict(compact=True), ["$1.23M", "−$5.43K"]),
]


Expand Down
Loading