當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。