本文整理汇总了Python中babel.numbers.format_percent函数的典型用法代码示例。如果您正苦于以下问题:Python format_percent函数的具体用法?Python format_percent怎么用?Python format_percent使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了format_percent函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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')
开发者ID:fsys,项目名称:babel,代码行数:7,代码来源:test_numbers.py
示例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)
开发者ID:noyonthe1,项目名称:shoop,代码行数:8,代码来源:shoop_common.py
示例3: percentage
def percentage(value):
if value or value == 0:
kwargs = {
'locale': to_locale(get_language()),
'format': "#,##0.00 %",
}
return format_percent(value, **kwargs)
开发者ID:dalou,项目名称:django-workon,代码行数:7,代码来源:extended.py
示例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})
开发者ID:gurch101,项目名称:shuup,代码行数:29,代码来源:charts.py
示例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)
开发者ID:Hubble1,项目名称:eventgrinder,代码行数:30,代码来源:i18n.py
示例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)
开发者ID:ahilles107,项目名称:Superdesk,代码行数:25,代码来源:text_conversion.py
示例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)
开发者ID:AaronJaramillo,项目名称:shopDPM,代码行数:8,代码来源:support.py
示例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)
开发者ID:AtomLaw,项目名称:Ally-Py,代码行数:10,代码来源:support.py
示例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)
开发者ID:dpgaspar,项目名称:flask-babelPkg,代码行数:10,代码来源:__init__.py
示例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)
开发者ID:jpscaletti,项目名称:allspeak,代码行数:14,代码来源:l10n.py
示例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)
开发者ID:lalinsky,项目名称:flask-babel,代码行数:15,代码来源:babel.py
示例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
开发者ID:beerm,项目名称:gratipay.com,代码行数:16,代码来源:i18n.py
示例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)
开发者ID:nagareproject,项目名称:core,代码行数:16,代码来源:i18n.py
示例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
开发者ID:SirCmpwn,项目名称:gratipay.com,代码行数:17,代码来源:i18n.py
示例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
开发者ID:maryannbanks,项目名称:www.gittip.com,代码行数:17,代码来源:i18n.py
示例16: add_helpers_to_context
def add_helpers_to_context(website, request):
context = request.context
loc = context["locale"] = get_locale_for_request(request, website)
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(website, 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
开发者ID:colindean,项目名称:www.gittip.com,代码行数:19,代码来源:i18n.py
示例17: 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['Money'] = Money
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_money(*a, locale=loc, **kw)
context['format_percent'] = lambda *a: format_percent(*a, locale=loc)
context['format_datetime'] = lambda *a: format_datetime(*a, locale=loc)
context['parse_decimal'] = lambda *a: parse_decimal(*a, locale=loc)
context['to_age'] = to_age
def to_age_str(o, **kw):
if not isinstance(o, datetime):
kw.setdefault('granularity', 'day')
return format_timedelta(to_age(o), locale=loc, **kw)
context['to_age_str'] = to_age_str
开发者ID:kthurimella,项目名称:liberapay.com,代码行数:19,代码来源:i18n.py
示例18: test_bar_chart_percent
def test_bar_chart_percent():
labels = ["One", "Two", "Three"]
locale = "pt_br"
chart = BarChart("ma biultiful xart %", labels, data_type=ChartDataType.PERCENT, locale=locale)
dataset1 = OrderedDict({"type": ChartType.BAR, "label": "some bars #1", "data": [0.1, 0.2, 0.3]})
dataset2 = OrderedDict({"type": ChartType.BAR, "label": "some bars #2", "data": [0.45, 0.55, .999]})
datasets = [dataset1, dataset2]
chart.add_data(dataset1["label"], dataset1["data"], dataset1["type"])
chart.add_data(dataset2["label"], dataset2["data"], dataset2["type"])
chart_config = chart.get_config()
assert chart_config["type"] == ChartType.BAR
assert chart_config["data"]["labels"] == labels
for i in range(len(chart_config["data"]["datasets"])):
for j in range(len(chart_config["data"]["datasets"][i]["data"])):
assert chart_config["data"]["datasets"][i]["data"][j] == datasets[i]["data"][j]
formatted_data = chart_config["data"]["datasets"][i]["formatted_data"][j]
assert formatted_data == format_percent(datasets[i]["data"][j], locale=locale)
开发者ID:gurch101,项目名称:shuup,代码行数:22,代码来源:test_chart.py
示例19: format_percent
def format_percent(number, format=None):
"""Return formatted percent value for a specific locale.
>>> 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:
>>> format_percent(25.1234, u'#,##0\u2030', locale='en_US')
u'25,123\u2030'
:param number:
The percent number to format
:param format:
:return:
The formatted percent number
"""
return numbers.format_percent(number, format=format, locale=local.locale)
开发者ID:aristidb,项目名称:cppbash,代码行数:22,代码来源:__init__.py
示例20: 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['Money'] = Money
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_money(*a, locale=loc, **kw)
context['format_percent'] = lambda *a: format_percent(*a, locale=loc)
context['format_datetime'] = lambda *a: format_datetime(*a, locale=loc)
context['get_lang_options'] = lambda *a, **kw: get_lang_options(context['request'], loc, *a, **kw)
context['to_age'] = to_age
def parse_decimal_or_400(s, *a):
try:
return parse_decimal(s, *a, locale=loc)
except (InvalidOperation, NumberFormatError, ValueError):
raise InvalidNumber(s)
context['parse_decimal'] = parse_decimal_or_400
def to_age_str(o, **kw):
if not isinstance(o, datetime):
kw.setdefault('granularity', 'day')
return format_timedelta(to_age(o), locale=loc, **kw)
context['to_age_str'] = to_age_str
def getdoc(name):
versions = context['website'].docs[name]
for lang in context['request'].accept_langs:
doc = versions.get(lang)
if doc:
return doc
return versions['en']
context['getdoc'] = getdoc
开发者ID:perfettiful,项目名称:liberapay.com,代码行数:39,代码来源:i18n.py
注:本文中的babel.numbers.format_percent函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论