IntlFormatter
DevExtreme-like number/date/datetime formatter.
Provides locale-aware formatting and parsing capabilities:
- FORMAT with Babel (locale-aware).
- PARSE dates/times with dateparser -> dateutil fallback.
- PARSE numbers with Babel's parse_decimal (+ compact K/M/B/T).
__init__
__init__(
locale: str | None = None,
*,
day_first: bool = False,
year_first: bool = False,
)
Create an IntlFormatter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
locale
|
str | None
|
Locale code like 'en_US' or 'de_DE'. If None, detect from the current process/system settings. |
None
|
day_first
|
bool
|
Whether day precedes month when parsing dates (e.g., D/M/Y). |
False
|
year_first
|
bool
|
Whether year precedes month/day when parsing dates (e.g., Y/M/D). |
False
|
format
format(value: Any, spec: FormatSpec) -> str
Format a number/date/time/datetime according to spec.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
The value to format. Numbers, date, time, datetime are handled specially; anything else is converted with str(). |
required |
spec
|
FormatSpec
|
Format specification. For numbers, a preset string like 'decimal', 'percent', 'currency', 'largeNumber', or a dict with options (e.g., {'type': 'percent', 'precision': 2}). For dates/times, use presets like 'longDate', 'shortTime', or a CLDR pattern via {'type': 'custom', 'pattern': 'yyyy-MM-dd'} (or simply the pattern string). |
required |
Returns:
| Type | Description |
|---|---|
str
|
The formatted string. |
parse
parse(text: str, spec: FormatSpec) -> Any
Parse a string into a number/date/time/datetime per spec.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Input string to parse. |
required |
spec
|
FormatSpec
|
A number or temporal spec, same shape as for format(). |
required |
Returns:
| Type | Description |
|---|---|
Any
|
Parsed Python object (float, date, time, datetime), or None if empty. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If parsing fails for temporal values. |