Example #1
0
def format_money(money,
                 format=None,
                 locale=LC_NUMERIC,
                 currency_digits=True,
                 format_type='standard',
                 decimal_quantization=True):
    """
    See https://babel.pocoo.org/en/latest/api/numbers.html
    """
    return babel_format_currency(money.amount,
                                 money.currency.code,
                                 format=format,
                                 locale=locale,
                                 currency_digits=currency_digits,
                                 format_type=format_type,
                                 decimal_quantization=decimal_quantization)
Example #2
0
def format_currency(context, currency_code, amount):
    locale = to_locale(context['request'].LANGUAGE_CODE)
    locale_obj = Locale.parse(locale)
    pattern = locale_obj.currency_formats['standard'].pattern

    # By default, Babel will display a fixed number of decimal places based on the
    # default format for the currency. It doesn't offer any way to tell
    # format_currency to hide decimals for integer values
    # see https://github.com/python-babel/babel/issues/478
    # In order to work around this, we fetch the pattern for the currency in
    # the current locale, and replace a padded decimal with an optional one.
    # We also have to set currency_digits=False otherwise this gets ignored entirely.
    if Decimal(amount) == int(amount):
        pattern = pattern.replace('0.00', '0.##')

    return babel_format_currency(
        amount, currency_code.upper(), format=pattern, locale=locale_obj, currency_digits=False
    )
Example #3
0
def format_money(
    money: "Money",
    format: Optional[str] = None,
    locale: str = LC_NUMERIC,
    currency_digits: bool = True,
    format_type: str = "standard",
    decimal_quantization: bool = True,
) -> str:
    """
    See https://babel.pocoo.org/en/latest/api/numbers.html
    """
    return babel_format_currency(  # type: ignore[no-any-return]
        money.amount,
        money.currency.code,
        format=format,
        locale=locale,
        currency_digits=currency_digits,
        format_type=format_type,
        decimal_quantization=decimal_quantization,
    )
Example #4
0
def format_currency(money, *args, **kwargs):
    return babel_format_currency(money.amount, money.currency.code, *args,
                                 **kwargs)