Skip to main content

format

Get a number (as a number or string), and return it as a string, using formats defined as arguments:

* decimal_sep: Decimal separator symbol (for example ".")
* decimal_pos: Number of decimal positions
* grouping: Number of digits in every group limited by thousand separator.
For non-uniform digit grouping, it can be a sequence with the number
of digit group sizes following the format used by the Python locale
module in locale.localeconv() LC_NUMERIC grouping (e.g. (3, 2, 0)).
* thousand_sep: Thousand separator symbol (for example ",")
def format(
number: Union[int, float, Decimal, str],
decimal_sep: str,
decimal_pos: int = None,
grouping: Union[int, Iterable[int]] = 0,
thousand_sep: str = "",
force_grouping: bool = False,
use_l10n: bool = None
) - > string

Get a number (as a number or string), and return it as a string, using formats defined as arguments.

Parameters

NameTypeDescription
numberUnion[int, float, Decimal, str]The numeric value or string representation of a number to be formatted.
decimal_sepstrThe character used as the decimal separator symbol (e.g., ".").
decimal_posint = NoneThe number of decimal positions to include; pads with zeros or truncates as necessary.
groupingUnion[int, Iterable[int]] = 0The number of digits in every group limited by the thousand separator, or a sequence of group sizes following locale.localeconv() rules.
thousand_sepstr = ""The character used as the thousand separator symbol (e.g., ",").
force_groupingbool = FalseIf True, applies the thousand separator grouping regardless of localization settings.
use_l10nbool = NoneDetermines if localization settings should be used to trigger grouping; defaults to True if not provided.

Returns

TypeDescription
stringThe formatted number as a string, including the specified separators, grouping, and decimal precision.