本文整理匯總了Python中babel.numbers.format_decimal方法的典型用法代碼示例。如果您正苦於以下問題:Python numbers.format_decimal方法的具體用法?Python numbers.format_decimal怎麽用?Python numbers.format_decimal使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類babel.numbers
的用法示例。
在下文中一共展示了numbers.format_decimal方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: format_number
# 需要導入模塊: from babel import numbers [as 別名]
# 或者: from babel.numbers import format_decimal [as 別名]
def format_number(number):
'''A locale aware formatter.'''
return format_decimal(number, locale=g.lang_code)
示例2: currencyfmt
# 需要導入模塊: from babel import numbers [as 別名]
# 或者: from babel.numbers import format_decimal [as 別名]
def currencyfmt(currency):
# We're only formatting ISK, so it has a bit of a special format
number = numbers.format_decimal(currency, format="#,##0.00;-#",
locale=get_locale())
return number
示例3: format_decimal
# 需要導入模塊: from babel import numbers [as 別名]
# 或者: from babel.numbers import format_decimal [as 別名]
def format_decimal(self, number, format=None):
"""Returns the given decimal number formatted for the current locale.
Example::
>>> format_decimal(1.2345, locale='en_US')
u'1.234'
>>> format_decimal(1.2346, locale='en_US')
u'1.235'
>>> format_decimal(-1.2346, locale='en_US')
u'-1.235'
>>> format_decimal(1.2345, locale='sv_SE')
u'1,234'
>>> format_decimal(12345, locale='de')
u'12.345'
The appropriate thousands grouping and the decimal separator are used
for each locale::
>>> format_decimal(12345.5, locale='en_US')
u'12,345.5'
:param number:
The number to format.
:param format:
Notation format.
:returns:
The formatted decimal number.
"""
return numbers.format_decimal(number, format=format,
locale=self.locale)
示例4: get_formatted_value
# 需要導入模塊: from babel import numbers [as 別名]
# 或者: from babel.numbers import format_decimal [as 別名]
def get_formatted_value(self, value, parameter, object_id, pattern=None, is_array_item=False):
rv = ''
if is_array_item and parameter.type == ParameterType.simple_array:
value_type = parameter.array_item_type
else:
value_type = parameter.type
if value_type == ParameterType.string:
rv = value
elif value_type in (ParameterType.number, ParameterType.average, ParameterType.sum):
if pattern:
used_pattern = pattern
pattern_has_currency = (pattern.find('$') != -1)
else:
used_pattern = parameter.pattern
pattern_has_currency = parameter.pattern_has_currency
if used_pattern:
try:
value = format_decimal(value, used_pattern, locale=self.pattern_locale)
if pattern_has_currency:
value = value.replace('$', self.pattern_currency_symbol)
rv = value
except ValueError:
error_object_id = object_id if pattern else parameter.id
raise ReportBroError(
Error('errorMsgInvalidPattern', object_id=error_object_id, field='pattern', context=value))
else:
rv = str(value)
elif value_type == ParameterType.date:
used_pattern = pattern if pattern else parameter.pattern
if used_pattern:
try:
rv = format_datetime(value, used_pattern, locale=self.pattern_locale)
except ValueError:
error_object_id = object_id if pattern else parameter.id
raise ReportBroError(
Error('errorMsgInvalidPattern',
object_id=error_object_id, field='pattern', context=value))
else:
rv = str(value)
return rv
示例5: format_price
# 需要導入模塊: from babel import numbers [as 別名]
# 或者: from babel.numbers import format_decimal [as 別名]
def format_price(price: Decimal) -> str:
if price is not None and price:
return format_decimal(price, locale='ru_RU')
return '0'