Пример #1
0
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
0
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
0
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
0
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
0
    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
0
    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
0
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
0
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
0
    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
0
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
Пример #11
0
 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)
Пример #12
0
def test_scientific_exponent_displayed_as_integer():
    assert numbers.format_scientific(100000, locale='en_US') == u'1E5'
Пример #13
0
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')
Пример #14
0
 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')
Пример #15
0
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')
Пример #16
0
def test_scientific_exponent_displayed_as_integer():
    assert numbers.format_scientific(100000, locale='en_US') == u'1E5'
Пример #17
0
 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, '1.2345E4')
     # 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.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')
Пример #18
0
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
Пример #19
0
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
Пример #20
0
 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)
Пример #21
0
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