class documentation

class Format: (source)

View In Hierarchy

Wrapper class providing the various date and number formatting functions bound to a specific locale and time-zone. >>> from babel.util import UTC >>> from datetime import date >>> fmt = Format('en_US', UTC) >>> fmt.date(date(2007, 4, 1)) u'Apr 1, 2007' >>> fmt.decimal(1.2345) u'1.234'

Method __init__ Initialize the formatter.
Method compact_currency Return a number in the given currency formatted for the locale using the compact number format.
Method compact_decimal Return a number formatted in compact form for the locale.
Method currency Return a number in the given currency formatted for the locale.
Method date Return a date formatted according to the given pattern.
Method datetime Return a date and time formatted according to the given pattern.
Method decimal Return a decimal number formatted for the locale.
Method number Return an integer number formatted for the locale.
Method percent Return a number formatted as percentage for the locale.
Method scientific Return a number formatted using scientific notation for the locale.
Method time Return a time formatted according to the given pattern.
Method timedelta Return a time delta according to the rules of the given locale.
Instance Variable locale Undocumented
Instance Variable tzinfo Undocumented
def __init__(self, locale: Locale|str, tzinfo: datetime.tzinfo|None = None): (source)

Initialize the formatter. :param locale: the locale identifier or `Locale` instance :param tzinfo: the time-zone info (a `tzinfo` instance or `None`)

def compact_currency(self, number: (float|decimal.Decimal)|str, currency: str, format_type: Literal['short'] = 'short', fraction_digits: int = 0) -> str: (source)

Return a number in the given currency formatted for the locale using the compact number format. >>> Format('en_US').compact_currency(1234567, "USD", format_type='short', fraction_digits=2) '$1.23M'

def compact_decimal(self, number: (float|decimal.Decimal)|str, format_type: Literal['short', 'long'] = 'short', fraction_digits: int = 0) -> str: (source)

Return a number formatted in compact form for the locale. >>> fmt = Format('en_US') >>> fmt.compact_decimal(123456789) u'123M' >>> fmt.compact_decimal(1234567, format_type='long', fraction_digits=2) '1.23 million'

def currency(self, number: (float|decimal.Decimal)|str, currency: str) -> str: (source)

Return a number in the given currency formatted for the locale.

def date(self, date: datetime.date|None = None, format: _PredefinedTimeFormat|str = 'medium') -> str: (source)

Return a date formatted according to the given pattern. >>> from datetime import date >>> fmt = Format('en_US') >>> fmt.date(date(2007, 4, 1)) u'Apr 1, 2007'

def datetime(self, datetime: datetime.date|None = None, format: _PredefinedTimeFormat|str = 'medium') -> str: (source)

Return a date and time formatted according to the given pattern. >>> from datetime import datetime >>> from babel.dates import get_timezone >>> fmt = Format('en_US', tzinfo=get_timezone('US/Eastern')) >>> fmt.datetime(datetime(2007, 4, 1, 15, 30)) u'Apr 1, 2007, 11:30:00 AM'

def decimal(self, number: (float|decimal.Decimal)|str, format: str|None = None) -> str: (source)

Return a decimal number formatted for the locale. >>> fmt = Format('en_US') >>> fmt.decimal(1.2345) u'1.234'

def number(self, number: (float|decimal.Decimal)|str) -> str: (source)

Return an integer number formatted for the locale. >>> fmt = Format('en_US') >>> fmt.number(1099) u'1,099'

def percent(self, number: (float|decimal.Decimal)|str, format: str|None = None) -> str: (source)

Return a number formatted as percentage for the locale. >>> fmt = Format('en_US') >>> fmt.percent(0.34) u'34%'

def scientific(self, number: (float|decimal.Decimal)|str) -> str: (source)

Return a number formatted using scientific notation for the locale.

def time(self, time: (datetime.time|datetime.datetime)|None = None, format: _PredefinedTimeFormat|str = 'medium') -> str: (source)

Return a time formatted according to the given pattern. >>> from datetime import datetime >>> from babel.dates import get_timezone >>> fmt = Format('en_US', tzinfo=get_timezone('US/Eastern')) >>> fmt.time(datetime(2007, 4, 1, 15, 30)) u'11:30:00 AM'

def timedelta(self, delta: datetime.timedelta|int, granularity: Literal['year', 'month', 'week', 'day', 'hour', 'minute', 'second'] = 'second', threshold: float = 0.85, format: Literal['narrow', 'short', 'medium', 'long'] = 'long', add_direction: bool = False) -> str: (source)

Return a time delta according to the rules of the given locale. >>> from datetime import timedelta >>> fmt = Format('en_US') >>> fmt.timedelta(timedelta(weeks=11)) u'3 months'

Undocumented

Undocumented