本文整理汇总了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"
示例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
示例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')
示例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)))
示例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)
示例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'}))
示例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'}))
示例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
示例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)
示例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
示例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)
示例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)
示例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
示例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)
示例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