当前位置: 首页>>代码示例>>Python>>正文


Python locale.format_string方法代码示例

本文整理汇总了Python中locale.format_string方法的典型用法代码示例。如果您正苦于以下问题:Python locale.format_string方法的具体用法?Python locale.format_string怎么用?Python locale.format_string使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在locale的用法示例。


在下文中一共展示了locale.format_string方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: testFormatFloat

# 需要导入模块: import locale [as 别名]
# 或者: from locale import format_string [as 别名]
def testFormatFloat():
    """
    Convert unit of scalar where value is "0.0".
    """
    assert FormatFloat("%g", 0.0) == "0"
    assert FormatFloat("%g", -0.0) == "0"

    scalar = Scalar("length", 0.0, "m")
    assert FormatFloat("%g", scalar.GetValue()) == "0"

    assert locale.format_string("%g", scalar.GetValue("ft")) == "-0"
    assert FormatFloat("%g", scalar.GetValue("ft")) == "0"

    # Large float numbers on integer format.
    large_float_number = 1e010 * 1.0
    assert FormatFloat("%d", large_float_number) == "10000000000"

    # Infinity
    assert FormatFloat("%.3g", PLUS_INFINITY) == "+INF"
    assert FormatFloat("%.3g", MINUS_INFINITY) == "-INF"
    assert FormatFloat("%.3g", NAN) == "-1.#IND"

    # Digit grouping
    assert FormatFloat("%.2f", 1234567, False) == "1234567.00"
    assert FormatFloat("%.2f", 1234567, True) == "1234567.00" 
开发者ID:ESSS,项目名称:barril,代码行数:27,代码来源:test_format_float.py

示例2: big_value_with_suffix

# 需要导入模块: import locale [as 别名]
# 或者: from locale import format_string [as 别名]
def big_value_with_suffix(value: float, decimals=3, strip_zeros=True) -> str:
        fmt_str = "%.{0:d}f".format(decimals)
        suffix = ""
        if abs(value) >= 1e9:
            suffix = "G"
            result = locale.format_string(fmt_str, value / 1e9)
        elif abs(value) >= 1e6:
            suffix = "M"
            result = locale.format_string(fmt_str, value / 1e6)
        elif abs(value) >= 1e3:
            suffix = "K"
            result = locale.format_string(fmt_str, value / 1e3)
        else:
            result = locale.format_string(fmt_str, value)

        if strip_zeros:
            result = result.rstrip("0").rstrip(Formatter.local_decimal_seperator())

        return result + suffix 
开发者ID:jopohl,项目名称:urh,代码行数:21,代码来源:Formatter.py

示例3: nice_number_default

# 需要导入模块: import locale [as 别名]
# 或者: from locale import format_string [as 别名]
def nice_number_default(the_number, **kwargs):
    """Returns the number as a word in the current language."""
    capitalize = kwargs.get('capitalize', False)
    language = kwargs.get('language', None)
    ensure_definition(the_number, capitalize, language)
    if language is None:
        language = this_thread.language
    if language in nice_numbers:
        language_to_use = language
    elif '*' in nice_numbers:
        language_to_use = '*'
    else:
        language_to_use = 'en'
    if int(float(the_number)) == float(the_number):
        the_number = int(float(the_number))
    if str(the_number) in nice_numbers[language_to_use]:
        the_word = nice_numbers[language_to_use][str(the_number)]
        if capitalize:
            return capitalize_function(the_word)
        else:
            return the_word
    elif type(the_number) is int:
        return str(locale.format_string("%d", the_number, grouping=True))
    else:
        return str(locale.format_string("%.2f", float(the_number), grouping=True)).rstrip('0') 
开发者ID:jhpyle,项目名称:docassemble,代码行数:27,代码来源:functions.py

示例4: test_int__format__locale

# 需要导入模块: import locale [as 别名]
# 或者: from locale import format_string [as 别名]
def test_int__format__locale(self):
        # test locale support for __format__ code 'n' for integers

        x = 123456789012345678901234567890
        for i in range(0, 30):
            self.assertEqual(locale.format_string('%d', x, grouping=True), format(x, 'n'))

            # move to the next integer to test
            x = x // 10

        rfmt = ">20n"
        lfmt = "<20n"
        cfmt = "^20n"
        for x in (1234, 12345, 123456, 1234567, 12345678, 123456789, 1234567890, 12345678900):
            self.assertEqual(len(format(0, rfmt)), len(format(x, rfmt)))
            self.assertEqual(len(format(0, lfmt)), len(format(x, lfmt)))
            self.assertEqual(len(format(0, cfmt)), len(format(x, cfmt))) 
开发者ID:bkerler,项目名称:android_universal,代码行数:19,代码来源:test_types.py

示例5: _test_format_string

# 需要导入模块: import locale [as 别名]
# 或者: from locale import format_string [as 别名]
def _test_format_string(self, format, value, out, **format_opts):
        self._test_formatfunc(format, value, out,
            func=locale.format_string, **format_opts) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:5,代码来源:test_locale.py

示例6: test_percent_escape

# 需要导入模块: import locale [as 别名]
# 或者: from locale import format_string [as 别名]
def test_percent_escape(self):
        self.assertEqual(locale.format_string('%f%%', 1.0), '%f%%' % 1.0)
        self.assertEqual(locale.format_string('%d %f%%d', (1, 1.0)),
            '%d %f%%d' % (1, 1.0))
        self.assertEqual(locale.format_string('%(foo)s %%d', {'foo': 'bar'}),
            ('%(foo)s %%d' % {'foo': 'bar'})) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:8,代码来源:test_locale.py

示例7: test_mapping

# 需要导入模块: import locale [as 别名]
# 或者: from locale import format_string [as 别名]
def test_mapping(self):
        self.assertEqual(locale.format_string('%(foo)s bing.', {'foo': 'bar'}),
            ('%(foo)s bing.' % {'foo': 'bar'}))
        self.assertEqual(locale.format_string('%(foo)s', {'foo': 'bar'}),
            ('%(foo)s' % {'foo': 'bar'})) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:7,代码来源:test_locale.py

示例8: format_data_short

# 需要导入模块: import locale [as 别名]
# 或者: from locale import format_string [as 别名]
def format_data_short(self, value):
        """return a short formatted string representation of a number"""
        if self._useLocale:
            return locale.format_string('%-12g', (value,))
        else:
            return '%-12g' % value 
开发者ID:ktraunmueller,项目名称:Computable,代码行数:8,代码来源:ticker.py

示例9: format_data

# 需要导入模块: import locale [as 别名]
# 或者: from locale import format_string [as 别名]
def format_data(self, value):
        'return a formatted string representation of a number'
        if self._useLocale:
            s = locale.format_string('%1.10e', (value,))
        else:
            s = '%1.10e' % value
        s = self._formatSciNotation(s)
        return self.fix_minus(s) 
开发者ID:ktraunmueller,项目名称:Computable,代码行数:10,代码来源:ticker.py

示例10: pprint_val

# 需要导入模块: import locale [as 别名]
# 或者: from locale import format_string [as 别名]
def pprint_val(self, x):
        xp = (x - self.offset) / (10. ** self.orderOfMagnitude)
        if np.absolute(xp) < 1e-8:
            xp = 0
        if self._useLocale:
            return locale.format_string(self.format, (xp,))
        else:
            return self.format % xp 
开发者ID:ktraunmueller,项目名称:Computable,代码行数:10,代码来源:ticker.py

示例11: format_string

# 需要导入模块: import locale [as 别名]
# 或者: from locale import format_string [as 别名]
def format_string(self, format, val, grouping=False):
        """
        Format a string in the current numeric locale. See python's
        locale.format_string for details.  ICU's message formatting codes are
        incompatible with locale's, so just use locale.format_string
        for now.
        """
        return locale.format_string(format, val, grouping) 
开发者ID:GenealogyCollective,项目名称:gprime,代码行数:10,代码来源:locale.py

示例12: __call__

# 需要导入模块: import locale [as 别名]
# 或者: from locale import format_string [as 别名]
def __call__(self, x, pos=None):
        """
        Return the format for tick value `x` at position `pos`.
        """
        if len(self.locs) == 0:
            return ''
        else:
            xp = (x - self.offset) / (10. ** self.orderOfMagnitude)
            if np.abs(xp) < 1e-8:
                xp = 0
            if self._useLocale:
                s = locale.format_string(self.format, (xp,))
            else:
                s = self.format % xp
            return self.fix_minus(s) 
开发者ID:PacktPublishing,项目名称:Mastering-Elasticsearch-7.0,代码行数:17,代码来源:ticker.py

示例13: format_data_short

# 需要导入模块: import locale [as 别名]
# 或者: from locale import format_string [as 别名]
def format_data_short(self, value):
        """
        Return a short formatted string representation of a number.
        """
        if self._useLocale:
            return locale.format_string('%-12g', (value,))
        else:
            return '%-12g' % value 
开发者ID:PacktPublishing,项目名称:Mastering-Elasticsearch-7.0,代码行数:10,代码来源:ticker.py

示例14: format_data

# 需要导入模块: import locale [as 别名]
# 或者: from locale import format_string [as 别名]
def format_data(self, value):
        """
        Return a formatted string representation of a number.
        """
        if self._useLocale:
            s = locale.format_string('%1.10e', (value,))
        else:
            s = '%1.10e' % value
        s = self._formatSciNotation(s)
        return self.fix_minus(s) 
开发者ID:PacktPublishing,项目名称:Mastering-Elasticsearch-7.0,代码行数:12,代码来源:ticker.py

示例15: pprint_val

# 需要导入模块: import locale [as 别名]
# 或者: from locale import format_string [as 别名]
def pprint_val(self, x):
        xp = (x - self.offset) / (10. ** self.orderOfMagnitude)
        if np.abs(xp) < 1e-8:
            xp = 0
        if self._useLocale:
            return locale.format_string(self.format, (xp,))
        else:
            return self.format % xp 
开发者ID:PacktPublishing,项目名称:Mastering-Elasticsearch-7.0,代码行数:10,代码来源:ticker.py


注:本文中的locale.format_string方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。