本文整理汇总了Python中babel.numbers.format_scientific函数的典型用法代码示例。如果您正苦于以下问题:Python format_scientific函数的具体用法?Python format_scientific怎么用?Python format_scientific使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了format_scientific函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_default_scientific_format
def test_default_scientific_format():
""" Check the scientific format method auto-correct the rendering pattern
in case of a missing fractional part.
"""
assert numbers.format_scientific(12345, locale='en_US') == u'1.2345E4'
assert numbers.format_scientific(12345.678, locale='en_US') == u'1.2345678E4'
assert numbers.format_scientific(12345, u'#E0', locale='en_US') == u'1.2345E4'
assert numbers.format_scientific(12345.678, u'#E0', locale='en_US') == u'1.2345678E4'
示例2: test_format_scientific
def test_format_scientific():
assert numbers.format_scientific(10000, locale='en_US') == u'1E4'
assert numbers.format_scientific(4234567, u'#.#E0', locale='en_US') == u'4.2E6'
assert numbers.format_scientific(4234567, u'0E0000', locale='en_US') == u'4.234567E0006'
assert numbers.format_scientific(4234567, u'##0E00', locale='en_US') == u'4.234567E06'
assert numbers.format_scientific(4234567, u'##00E00', locale='en_US') == u'42.34567E05'
assert numbers.format_scientific(4234567, u'0,000E00', locale='en_US') == u'4,234.567E03'
assert numbers.format_scientific(4234567, u'##0.#####E00', locale='en_US') == u'4.23457E06'
assert numbers.format_scientific(4234567, u'##0.##E00', locale='en_US') == u'4.23E06'
assert numbers.format_scientific(42, u'00000.000000E0000', locale='en_US') == u'42000.000000E-0003'
示例3: format_scientific
def format_scientific(number, format=None, locale=None):
"""Returns value formatted in scientific notation for a specific locale.
.. code-block:: python
>>> format_scientific(10000, locale='en_US')
u'1E4'
The format pattern can also be specified explicitly:
.. code-block:: python
>>> format_scientific(1234567, u'##0E00', locale='en_US')
u'1.23E06'
:param number:
The number to format.
:param format:
Notation format.
:param locale:
A locale code. If not set, uses the currently loaded locale.
:returns:
Value formatted in scientific notation.
"""
locale = locale or get_locale()
return numbers.format_scientific(number, format=format, locale=locale)
示例4: format_scientific
def format_scientific(number, format=None):
"""Return value formatted in scientific notation 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_scientific(number, format=format, locale=locale)
示例5: format_scientific
def format_scientific(self, number, format=None):
"""Return value formatted in scientific notation
>>> Locale('en', 'US').format_scientific(10000)
u'1E4'
The format pattern can also be specified explicitly:
>>> Locale('en', 'US').format_scientific(1234567, u'##0E00')
u'1.23E06'
"""
return numbers.format_scientific(number, format, self)
示例6: format_scientific
def format_scientific(self, number, format=None, locale=None, **kwargs):
"""Return value formatted in scientific notation 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_scientific(number, format=format, locale=locale, **kwargs)
示例7: format_scientific
def format_scientific(number, format=None):
"""Return a decimal number formatted in scientific notation.
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 `scientificformat`.
.. _`Babel documentation`: http://babel.edgewall.org/wiki/Documentation/numbers.html#pattern-syntax
"""
locale = get_locale()
return numbers.format_scientific(number, format=format, locale=locale)
示例8: format_scientific
def format_scientific(number, format=None):
"""Return value formatted in scientific notation for a specific locale.
>>> format_scientific(10000, locale='en_US')
u'1E4'
The format pattern can also be specified explicitly:
>>> format_scientific(1234567, u'##0E00', locale='en_US')
u'1.23E06'
:param number:
The number to format.
:param format:
:return:
Value formatted in scientific notation.
"""
return numbers.format_scientific(number, format=format, locale=local.locale)
示例9: format_scientific
def format_scientific(self, number, format=None):
"""Returns value formatted in scientific notation for the current
locale. Example::
>>> format_scientific(10000, locale='en_US')
u'1E4'
The format pattern can also be specified explicitly::
>>> format_scientific(1234567, u'##0E00', locale='en_US')
u'1.23E06'
:param number:
The number to format.
:param format:
Notation format.
:returns:
Value formatted in scientific notation.
"""
return numbers.format_scientific(number, format=format,
locale=self.locale)
示例10: scientific
def scientific(self, number):
"""Return a number formatted using scientific notation for the locale.
:see: `babel.numbers.format_scientific`
"""
return format_scientific(number, locale=self.locale)
示例11: test_scientific_notation
def test_scientific_notation(self):
fmt = numbers.format_scientific(0.1, '#E0', locale='en_US')
self.assertEqual(fmt, '1E-1')
fmt = numbers.format_scientific(0.01, '#E0', locale='en_US')
self.assertEqual(fmt, '1E-2')
fmt = numbers.format_scientific(10, '#E0', locale='en_US')
self.assertEqual(fmt, '1E1')
fmt = numbers.format_scientific(1234, '0.###E0', locale='en_US')
self.assertEqual(fmt, '1.234E3')
fmt = numbers.format_scientific(1234, '0.#E0', locale='en_US')
self.assertEqual(fmt, '1.2E3')
# Exponent grouping
fmt = numbers.format_scientific(12345, '##0.####E0', locale='en_US')
self.assertEqual(fmt, '12.345E3')
# Minimum number of int digits
fmt = numbers.format_scientific(12345, '00.###E0', locale='en_US')
self.assertEqual(fmt, '12.345E3')
fmt = numbers.format_scientific(-12345.6, '00.###E0', locale='en_US')
self.assertEqual(fmt, '-12.346E3')
fmt = numbers.format_scientific(-0.01234, '00.###E0', locale='en_US')
self.assertEqual(fmt, '-12.34E-3')
# Custom pattern suffic
fmt = numbers.format_scientific(123.45, '#.##E0 m/s', locale='en_US')
self.assertEqual(fmt, '1.23E2 m/s')
# Exponent patterns
fmt = numbers.format_scientific(123.45, '#.##E00 m/s', locale='en_US')
self.assertEqual(fmt, '1.23E02 m/s')
fmt = numbers.format_scientific(0.012345, '#.##E00 m/s', locale='en_US')
self.assertEqual(fmt, '1.23E-02 m/s')
fmt = numbers.format_scientific(Decimal('12345'), '#.##E+00 m/s',
locale='en_US')
self.assertEqual(fmt, '1.23E+04 m/s')
# 0 (see ticket #99)
fmt = numbers.format_scientific(0, '#E0', locale='en_US')
self.assertEqual(fmt, '0E0')
示例12: test_format_scientific
def test_format_scientific():
assert numbers.format_scientific(10000, locale='en_US') == u'1E4'
assert (numbers.format_scientific(1234567, u'##0E00', locale='en_US')
== u'1.23E06')
示例13: test_scientific_exponent_displayed_as_integer
def test_scientific_exponent_displayed_as_integer():
assert numbers.format_scientific(100000, locale='en_US') == u'1E5'
示例14: test_format_scientific_quantization
def test_format_scientific_quantization():
# Test all locales.
for locale_code in localedata.locale_identifiers():
assert numbers.format_scientific(
'0.9999999999', locale=locale_code, decimal_quantization=False).find('999999999') > -1
示例15: test_format_scientific_precision
def test_format_scientific_precision(input_value, expected_value):
# Test precision conservation.
assert numbers.format_scientific(
decimal.Decimal(input_value), locale='en_US', decimal_quantization=False) == expected_value