本文整理汇总了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'