本文整理汇总了Python中babel.numbers.format_percent函数的典型用法代码示例。如果您正苦于以下问题:Python format_percent函数的具体用法?Python format_percent怎么用?Python format_percent使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了format_percent函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_format_percent
def test_format_percent():
assert numbers.format_percent(0.34, locale='en_US') == '34%'
assert numbers.format_percent(25.1234, locale='en_US') == '2,512%'
assert (numbers.format_percent(25.1234, locale='sv_SE')
== '2\xa0512\xa0%')
assert (numbers.format_percent(25.1234, '#,##0\u2030', locale='en_US')
== '25,123\u2030')
示例2: percent
def percent(value, ndigits=3):
locale = get_current_babel_locale()
if not ndigits:
return format_percent(value, locale=locale)
else:
format = locale.percent_formats.get(None)
new_fmt = format.pattern.replace("0", "0." + (ndigits * "#"))
return format_percent(value, format=new_fmt, locale=locale)
示例3: percentage
def percentage(value):
if value or value == 0:
kwargs = {
'locale': to_locale(get_language()),
'format': "#,##0.00 %",
}
return format_percent(value, **kwargs)
示例4: add_data
def add_data(self, name, data, chart_type):
"""
Add data to this chart
:param name: the name of the dataset
:type name: str
:param data: the list of data
:type data: list[int|float|Decimal]
:param chart_type: the chart type - tells how data should be rendered.
This data type must be available in the `supported_chart_type` attribute of this instance
:type chart_type: ChartType
"""
assert chart_type in self.supported_chart_types
formatted_data = []
# format value for each data point
if self.data_type == ChartDataType.CURRENCY:
for value in data:
formatted_data.append(format_money(Money(value, currency=self.currency).as_rounded()))
elif self.data_type == ChartDataType.PERCENT:
for value in data:
formatted_data.append(format_percent(value, locale=self.locale))
# self.data_type == ChartDataType.NUMBER
else:
for value in data:
formatted_data.append(format_decimal(value, locale=self.locale))
self.datasets.append({"type": chart_type, "label": name, "data": data, "formatted_data": formatted_data})
示例5: format_percent
def format_percent(number, format=None, locale=None):
"""Returns formatted percent value for a specific locale.
.. code-block:: python
>>> format_percent(0.34, locale='en_US')
u'34%'
>>> format_percent(25.1234, locale='en_US')
u'2,512%'
>>> format_percent(25.1234, locale='sv_SE')
u'2\\xa0512\\xa0%'
The format pattern can also be specified explicitly:
.. code-block:: python
>>> format_percent(25.1234, u'#,##0\u2030', locale='en_US')
u'25,123\u2030'
:param number:
The percent number to format
:param format:
Notation format.
:param locale:
A locale code. If not set, uses the currently loaded locale.
:returns:
The formatted percent number.
"""
locale = locale or get_locale()
return numbers.format_percent(number, format=format, locale=locale)
示例6: asString
def asString(self, objValue, objType):
'''
@see: Converter.asString
'''
assert isinstance(objType, Type), 'Invalid object type %s' % objType
if isinstance(objType, TypeModel): # If type model is provided we consider the model property type
assert isinstance(objType, TypeModel)
container = objType.container
assert isinstance(container, Model)
objType = container.properties[container.propertyId]
if objType.isOf(str):
return objValue
if objType.isOf(bool):
return str(objValue)
if objType.isOf(Percentage):
return bn.format_percent(objValue, self.formats.get(Percentage, None), self.locale)
if objType.isOf(Number):
return bn.format_decimal(objValue, self.formats.get(Number, None), self.locale)
if objType.isOf(Date):
return bd.format_date(objValue, self.formats.get(Date, None), self.locale)
if objType.isOf(Time):
return bd.format_time(objValue, self.formats.get(Time, None), self.locale)
if objType.isOf(DateTime):
return bd.format_datetime(objValue, self.formats.get(DateTime, None), None, self.locale)
raise TypeError('Invalid object type %s for Babel converter' % objType)
示例7: percent
def percent(self, number, format=None):
"""Return a number formatted as percentage for the locale.
>>> fmt = Format('en_US')
>>> fmt.percent(0.34)
u'34%'
"""
return format_percent(number, format, locale=self.locale)
示例8: percent
def percent(self, number, format=None):
"""Return a number formatted as percentage for the locale.
>>> fmt = Format('en_US')
>>> fmt.percent(0.34) == u('34%')
True
:see: `babel.numbers.format_percent`
"""
return format_percent(number, format, locale=self.locale)
示例9: format_percent
def format_percent(number, format=None):
"""Return formatted percent value for the locale in request
:param number: the number to format
:param format: the format to use
:return: the formatted percent number
:rtype: unicode
"""
locale = get_locale()
return numbers.format_percent(number, format=format, locale=locale)
示例10: format_percent
def format_percent(self, number, format=None, locale=None, **kwargs):
"""Return a percent value formatted for the locale in the
current request.
:param number: the number to format
:param format: the format to use as
`documented by Babel <http://babel.pocoo.org/docs/numbers/#pattern-syntax>`_.
:param locale: Overwrite the global locale.
"""
if number in ('', None):
return ''
locale = utils.normalize_locale(locale) or self.get_locale()
return numbers.format_percent(number, format=format, locale=locale, **kwargs)
示例11: format_percent
def format_percent(number, format=None):
"""Return a formatted percent value.
If you specify just the number, it uses the default format pattern
for the current locale. The format parameter can be used to force a
custom pattern. See the `Babel documentation`_ for details on the
pattern syntax.
This function is also available in the template context as filter
named `percentformat`.
.. _`Babel documentation`: http://babel.edgewall.org/wiki/Documentation/numbers.html#pattern-syntax
"""
locale = get_locale()
return numbers.format_percent(number, format=format, locale=locale)
示例12: add_helpers_to_context
def add_helpers_to_context(tell_sentry, context, loc, request=None):
context['locale'] = loc
context['decimal_symbol'] = get_decimal_symbol(locale=loc)
context['_'] = lambda s, *a, **kw: get_text(loc, s, *a, **kw)
context['ngettext'] = lambda *a, **kw: n_get_text(tell_sentry, request, loc, *a, **kw)
context['format_number'] = lambda *a: format_number(*a, locale=loc)
context['format_decimal'] = lambda *a: format_decimal(*a, locale=loc)
context['format_currency'] = lambda *a, **kw: format_currency_with_options(*a, locale=loc, **kw)
context['format_percent'] = lambda *a: format_percent(*a, locale=loc)
context['parse_decimal'] = lambda *a: parse_decimal(*a, locale=loc)
def _to_age(delta):
try:
return to_age(delta, loc)
except:
return to_age(delta, 'en')
context['to_age'] = _to_age
示例13: format_percent
def format_percent(self, number, format=None):
"""Return formatted percent value
>>> Locale('en', 'US').format_percent(0.34)
u'34%'
>>> Locale('en', 'US').format_percent(25.1234)
u'2,512%'
>>> Locale('sv', 'SE').format_percent(25.1234)
u'2\\xa0512\\xa0%'
The format pattern can also be specified explicitly:
>>> Locale('en', 'US').format_percent(25.1234, u'#,##0\u2030')
u'25,123\u2030'
"""
return numbers.format_percent(number, format, self)
示例14: add_helpers_to_context
def add_helpers_to_context(tell_sentry, context, loc):
context['escape'] = lambda s: s # to be overriden by renderers
context['locale'] = loc
context['decimal_symbol'] = get_decimal_symbol(locale=loc)
context['_'] = lambda s, *a, **kw: get_text(context, loc, s, *a, **kw)
context['ngettext'] = lambda *a, **kw: n_get_text(tell_sentry, context, loc, *a, **kw)
context['format_number'] = lambda *a: format_number(*a, locale=loc)
context['format_decimal'] = lambda *a: format_decimal(*a, locale=loc)
context['format_currency'] = lambda *a, **kw: format_currency_with_options(*a, locale=loc, **kw)
context['format_percent'] = lambda *a: format_percent(*a, locale=loc)
context['parse_decimal'] = lambda *a: parse_decimal(*a, locale=loc)
def _to_age(delta, **kw):
try:
return to_age(delta, loc, **kw)
except:
return to_age(delta, 'en', **kw)
context['to_age'] = _to_age
示例15: inbound
def inbound(request):
context = request.context
loc = context.locale = get_locale_for_request(request)
context.decimal_symbol = get_decimal_symbol(locale=loc)
context._ = lambda s, *a, **kw: get_text(request, loc, s, *a, **kw)
context.ngettext = lambda *a, **kw: n_get_text(request, loc, *a, **kw)
context.format_number = lambda *a: format_number(*a, locale=loc)
context.format_decimal = lambda *a: format_decimal(*a, locale=loc)
context.format_currency = lambda *a: format_currency(*a, locale=loc)
context.format_percent = lambda *a: format_percent(*a, locale=loc)
context.parse_decimal = lambda *a: parse_decimal(*a, locale=loc)
def _to_age(delta):
try:
return to_age(delta, loc)
except:
return to_age(delta, 'en')
context.to_age = _to_age