本文整理汇总了Python中babel.numbers.format_currency函数的典型用法代码示例。如果您正苦于以下问题:Python format_currency函数的具体用法?Python format_currency怎么用?Python format_currency使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了format_currency函数的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: money_format
def money_format(req, amount, code=None, prefix=None, suffix=None,
currency=None):
if amount is None:
return ''
cfg = req.registry.settings
loc = req.current_locale
if currency is not None:
code = currency.code
prefix = currency.prefix
suffix = currency.suffix
if code is None:
code = cfg.get('netprofile.currency.default')
if code is None:
formatted = format_decimal(amount, locale=loc)
elif code in loc.currencies:
formatted = format_currency(amount, code, locale=loc)
else:
stdloc = req.locales[cfg.get('pyramid.default_locale_name', 'en')]
if code in stdloc.currencies:
formatted = format_currency(amount, code, locale=stdloc)
else:
formatted = format_decimal(amount, locale=loc)
ret = []
if prefix:
ret.append(prefix)
ret.append(formatted)
if suffix:
ret.append(suffix)
return '\xa0'.join(ret)
示例2: money_format_long
def money_format_long(req, amount, code=None, currency=None):
if amount is None:
return ''
cfg = req.registry.settings
loc = req.current_locale
if currency is not None:
code = currency.code
if code is None:
code = cfg.get('netprofile.currency.default')
if code is not None:
if code in loc.currencies:
return format_currency(amount, code, locale=loc,
format='0.######## ¤¤¤',
currency_digits=False)
else:
stdloc = req.locales[cfg.get('pyramid.default_locale_name', 'en')]
if code in stdloc.currencies:
return format_currency(amount, code, locale=stdloc,
format='0.######## ¤¤¤',
currency_digits=False)
return format_decimal(amount, locale=loc, format='0.########')
示例3: moneyfy
def moneyfy(currency, amount):
'''Format amount of money according to a locale'''
if currency.lower() in ('€', 'eur'):
return format_currency(int(amount), 'EUR', locale='en_GB').split('.')[0]
elif currency.lower() in ('£', 'gbp'):
return format_currency(int(amount), 'GBP', locale='en_GB').split('.')[0]
return amount
示例4: currency_format
def currency_format(context, value, currency='EUR', decimal=2):
if value is None:
value = 0.0
lang = translation.get_language()
locale = context.get('locale', lang)
if isinstance(value, six.string_types):
symbol = get_currency_symbol(currency, locale=locale)
if not symbol:
symbol = currency
return mark_safe('%s %s' % (value, symbol))
value = round(value, decimal)
formatted = format_currency(value, currency, locale=locale)
symbol = get_currency_symbol(currency, locale=locale)
if not context.get('filter_country') and len(symbol) == 1:
# When we have possibly mixed currencies, show three letter symbol
# This improves alignment
formatted = formatted.replace(symbol, currency)
if decimal == 0:
zero_amount = format_currency(0, currency, locale=locale)
zero_decimals = zero_amount.replace(symbol, '').strip()[1:]
formatted = formatted.replace(zero_decimals, '')
return mark_safe(formatted.replace(' ', ' '))
示例5: test_format_currency_long_display_name
def test_format_currency_long_display_name():
assert (numbers.format_currency(1099.98, 'USD', locale='en_US', format_type='name')
== u'1,099.98 US dollars')
assert (numbers.format_currency(1.00, 'USD', locale='en_US', format_type='name')
== u'1.00 US dollar')
assert (numbers.format_currency(1.00, 'EUR', locale='en_US', format_type='name')
== u'1.00 euro')
assert (numbers.format_currency(2, 'EUR', locale='en_US', format_type='name')
== u'2.00 euros')
# This tests that '{1} {0}' unitPatterns are found:
assert (numbers.format_currency(1, 'USD', locale='sw', format_type='name')
== u'dola ya Marekani 1.00')
# This tests unicode chars:
assert (numbers.format_currency(1099.98, 'USD', locale='es_GT', format_type='name')
== u'dólares estadounidenses 1,099.98')
# Test for completely unknown currency, should fallback to currency code
assert (numbers.format_currency(1099.98, 'XAB', locale='en_US', format_type='name')
== u'1,099.98 XAB')
# Test for finding different unit patterns depending on count
assert (numbers.format_currency(1, 'USD', locale='ro', format_type='name')
== u'1,00 dolar american')
assert (numbers.format_currency(2, 'USD', locale='ro', format_type='name')
== u'2,00 dolari americani')
assert (numbers.format_currency(100, 'USD', locale='ro', format_type='name')
== u'100,00 de dolari americani')
示例6: test_format_currency_format_type
def test_format_currency_format_type():
assert (numbers.format_currency(1099.98, 'USD', locale='en_US',
format_type="standard")
== u'$1,099.98')
assert (numbers.format_currency(0, 'USD', locale='en_US',
format_type="standard")
== u'$0.00')
assert (numbers.format_currency(1099.98, 'USD', locale='en_US',
format_type="accounting")
== u'$1,099.98')
assert (numbers.format_currency(0, 'USD', locale='en_US',
format_type="accounting")
== u'$0.00')
with pytest.raises(numbers.UnknownCurrencyFormatError) as excinfo:
numbers.format_currency(1099.98, 'USD', locale='en_US',
format_type='unknown')
assert excinfo.value.args[0] == "'unknown' is not a known currency format type"
assert (numbers.format_currency(1099.98, 'JPY', locale='en_US')
== u'\xa51,100')
assert (numbers.format_currency(1099.98, 'COP', u'#,##0.00', locale='es_ES')
== u'1.100')
assert (numbers.format_currency(1099.98, 'JPY', locale='en_US',
currency_digits=False)
== u'\xa51,099.98')
assert (numbers.format_currency(1099.98, 'COP', u'#,##0.00', locale='es_ES',
currency_digits=False)
== u'1.099,98')
示例7: currency_string
def currency_string(value, currency):
"""Takes a value and currency code and uses babel
to properly format the currency into a localized string"""
if value < 0:
return "-" + format_currency(abs(value), currency)
else:
return format_currency(value, currency)
示例8: list_splits
def list_splits(trans):
"""Returns a string with a list of splits for the given transaction
(trans)"""
temp = ""
for split in trans.splits:
if split.reconcile_state == "y":
temp +="\t* "
elif split.reconcile_state == "c":
temp +="\t! "
else:
temp += "\t"
temp += "{:60s}\t".format(full_acc_name(split.account))
if split.account.commodity != trans.currency and args.posting_cost == True:
temp += "{:f} {} @@ {}".format(split.quantity,
split.account.commodity,
format_currency(
abs(split.value), str(trans.currency)))
elif split.account.commodity != trans.currency and args.posting_cost == False:
temp += "{:f} {} @ {}".format(split.quantity,
split.account.commodity,
format_currency(
abs(split.commodity_price),
str(trans.currency)))
else:
temp += "{}".format(currency_string(split.value, str(trans.currency)))
if split.memo:
temp += "\t; {}".format(split.memo)
temp += "\n"
return temp
示例9: make_campaign_table_row
def make_campaign_table_row(cls, id, start, end, target, bid, impressions,
clicks, is_live, is_active, url, is_total):
if impressions:
cpm = format_currency(promote.cost_per_mille(bid, impressions),
'USD', locale=c.locale)
else:
cpm = '---'
if clicks:
cpc = format_currency(promote.cost_per_click(bid, clicks), 'USD',
locale=c.locale)
ctr = format_number(_clickthrough_rate(impressions, clicks))
else:
cpc = '---'
ctr = '---'
return {
'id': id,
'start': start,
'end': end,
'target': target,
'bid': format_currency(bid, 'USD', locale=c.locale),
'impressions': format_number(impressions),
'cpm': cpm,
'clicks': format_number(clicks),
'cpc': cpc,
'ctr': ctr,
'live': is_live,
'active': is_active,
'url': url,
'csv': url + '.csv',
'total': is_total,
}