def selftest(): tests = 0, 1 # fails: -1. 1.23 tests += 12, 123, 1234, 12345, 123456, 1234567 tests += 2**32, 2**100 for test in tests: print(commas(test)) print('') tests = 0, 1, -1, 1.23, 1., 1.2, 3.14159 tests += 12.34, 12.344, 12.345, 12.346 tests += 2**32, (2**32 + .2345) tests += 1.2345, 1.2, 0.2345 tests += -1.2345, -1.2, -0.2345 tests += -(2**32), -(2**32 + .2345) tests += (2**100), -(2**100) for test in tests: print('%s [%s]' % (money(test, 17), test))
from __future__ import print_function # 2.X from formats import money X = 54321.987 print(money(X), money(X, 0, '')) print(money(X, currency=u'\xA3'), money(X, currency=u'\u00A5')) print(money(X, currency=b'\xA3'.decode('latin-1'))) print(money(X, currency=u'\u20AC'), money(X, 0, b'\xA4'.decode('iso-8859-15'))) print(money(X, currency=b'\xA4'.decode('latin-1')))
# -*- coding: latin-1 -*- from formats import money X = 54321.987 print(money(X), money(X, 10, '')) result = "'$54,321.99', ' 54,321.99'" print(money(X, currency=u'\xA3'), money(X, currency=u'\u00A5')) # '�,321.99', '�,321.99' result = "'�,321.99', '�,321.99'" print(money(X, currency=b'\xA3'.decode('latin-1'))) # '�,321.99' #print(money(X, currency=u'\u20AC'), money(X, currency=b'\xA4'.decode('iso-8859-15'))) # '�54,321.99', '�54,321.99'
from formats import money x = 54321.987 print(money(x), money(x, 0, '')) print(money(x, currency=u'\xa3'), money(x, currency=u'\u00a5')) print(money(x, currency=b'\xa3'.decode('latin-1'))) print(money(x, currency=u'\u20ac'), money(x, 0, b'\xa4'.decode('iso-8859-15'))) print(money(x, currency=b'\xa4'.decode('latin-1')))
print('') tests = 0, 1, -1, 1.23, 1., 1.2, 3.14159 tests += 12.34, 12.344, 12.345, 12.346 tests += 2**32, (2**32 + .2345) tests += 1.2345, 1.2, 0.2345 tests += -1.2345, -1.2, -0.2345 tests += -(2**32), -(2**32 + .2345) tests += (2**100), -(2**100) for test in tests: print('%s [%s]' % (money(test, 17), test)) import sys if len(sys.argv) == 1: selftest() else: print(money(float(sys.argv[1]), int(sys.argv[2]))) # python formats.py # 01 # 12 # 123 # 1,234 # 12,345 # 123,456 # 1,234,567 # ...등등... # python formats.py 999999999 0 # $999,999,999.00 # python formats.py -999999999 0 # $-999,999,999.00
print('%e, %.3e, %g' % (3.14159, 3.14159, 3.14159)) print('%f, %.2f, %06.2f' % (3.14159, 3.14159, 3.14159)) # Hex and octal, but not binary (see ahead) print('#' * 52 + ' Hex and octal, but not binary (see ahead)') print('%x, %o' % (255, 255)) # Hardcoded references in both print('#' * 52 + ' Hardcoded references in both') import sys print('My {1[kind]:<8} runs {0.platform:>8}'.format(sys, {'kind': 'laptop'})) print('My %(kind)-8s runs %(plat)8s' % dict(kind='laptop', plat=sys.platform)) # Building data ahead of time in both print('#' * 52 + ' Building data ahead of time in both') data = dict(platform=sys.platform, kind='laptop') print('My {kind:<8} runs {platform:>8}'.format(**data)) print('My %(kind)-8s runs %(platform)8s' % data) print('{0:d}'.format(999999999999)) print('{0:,d}'.format(999999999999)) print('{:,d}'.format(999999999999)) print('{:,d} {:,d}'.format(9999999, 8888888)) print('{:,.2f}'.format(296999.2567)) from formats import commas, money print('%s' % commas(999999999999)) print('%s %s' % (commas(9999999), commas(8888888))) print('%s' % money(296999.2567)) print([commas(x) for x in (9999999, 8888888)]) print('%s %s' % tuple(commas(x) for x in (9999999, 8888888))) print(''.join(commas(x) for x in (9999999, 8888888)))
# 파이썬 3.1과 2.7에서 문자열 포맷 메서드 개선 사항들 '{0:d}'.format(999999999999) # '999999999999' '{0:,d}'.format(999999999999) # '999,999,999,999' '{:,d}'.format(999999999999) # '999,999,999,999' '{:,d} {:,d}'.format(9999999, 8888888) # '9,999,999 8,888,888' '{:,.2f}'.format(296999.2567) # '296,999.26' from formats import commas, money '%s' % commas(999999999999) # '999,999,999,999' '%s %s' % (commas(9999999), commas(8888888)) # '9,999,999 8,888,888' '%s' % money(296999.2567) # '$296,999.26' [commas(x) for x in (9999999, 8888888)] # ['9.999.999', '8,888,888'] '%s %s' % tuple(commas(x) for x in (9999999, 8888888)) # '9,999,999 8,888,888' ''.join(commas(x) for x in (9999999, 8888888)) # '9,999,999 8,888,888'
from formats import money, commas print(money(123.456)) print(money(123.456, 0, u'\xa3')) print(money(123.456, 0, u'\u00a3')) print(money(-9999999.99, 15)) X = 99999999999999999999 print('%s (%s)' % (commas(X), X))
from formats import money, commas print(money(123.456)) print(money(-9999999.99, 15)) X = 99999999999999999999999 print('%s (%s)' % (commas(X), X))