web-dev-qa-db-ja.com

桁区切り記号としてカンマで番号を印刷する方法?

Python 2.6.1 の整数を、桁区切り記号としてカンマを使って表示しようとしています。たとえば、1234567という数字を1,234,567として表示します。これをどのようにして行うのでしょうか。私はGoogleで多くの例を見ましたが、私は最も簡単で実用的な方法を探しています。

ピリオドとコンマの間で決定するためにロケール固有である必要はありません。私は合理的に可能な限り簡単なものを好むでしょう。

586
Elias Zamaria

Python≧2.7の場合:

"{:,}".format(value)

フォーマット仕様ミニ言語

','オプションは、千単位の区切り文字にカンマを使用することを示します。ロケール対応の区切り文字の場合は、代わりに'n'整数表示タイプを使用してください。

1490
Ian Schneider

私はこれを機能させることができました:

>>> import locale
>>> locale.setlocale(locale.LC_ALL, 'en_US')
'en_US'
>>> locale.format("%d", 1255000, grouping=True)
'1,255,000'

確かに、あなたは国際化をサポートしていません 必要ではありません しかし、それは明確で、簡潔で、そして組み込みのライブラリを使用しています。

P.Sその "%d"は通常の%スタイルのフォーマッタです。フォーマッタは1つしか持てませんが、フィールド幅と精度の設定に関しては必要なものは何でも構いません。

P.P.S.もしあなたがlocaleを動作させることができないならば、私はMarkの答えの修正版をお勧めします:

def intWithCommas(x):
    if type(x) not in [type(0), type(0L)]:
        raise TypeError("Parameter must be an integer.")
    if x < 0:
        return '-' + intWithCommas(-x)
    result = ''
    while x >= 1000:
        x, r = divmod(x, 1000)
        result = ",%03d%s" % (r, result)
    return "%d%s" % (x, result)

再帰は否定的な場合に役立ちますが、コンマごとに1回の再帰は私には多すぎると思われます。

272
Mike DeSimone

非効率と判読不能のためにそれを打つのは難しいです:

>>> import itertools
>>> s = '-1234567'
>>> ','.join(["%s%s%s" % (x[0], x[1] or '', x[2] or '') for x in itertools.izip_longest(s[::-1][::3], s[::-1][1::3], s[::-1][2::3])])[::-1].replace('-,','-')
107
Kasey Kirkham

無関係な部分を削除して少し整理した後のロケールグループ化コードは次のとおりです。

(以下は整数に対してのみ有効です)

def group(number):
    s = '%d' % number
    groups = []
    while s and s[-1].isdigit():
        groups.append(s[-3:])
        s = s[:-3]
    return s + ','.join(reversed(groups))

>>> group(-23432432434.34)
'-23,432,432,434'

ここにはすでにいくつかの良い答えがあります。将来の参考のためにこれを追加したいだけです。 Python 2.7では、千単位の区切り文字のフォーマット指定子が追加される予定です。 python docsによると それはこのように動作します

>>> '{:20,.2f}'.format(f)
'18,446,744,073,709,551,616.00'

Python3.1では、これと同じことができます。

>>> format(1234567, ',d')
'1,234,567'
91
Nadia Alramli

Python 3.6では、これと同じくらい簡単にf文字列を使用してこれを実行できるとは誰も述べていません。

>>> num = 10000000
>>> print(f"{num:,d}")
10,000,000

コロンの後の部分はフォーマット指定子です。コンマは必要な区切り文字なので、f"{num:_d}"はコンマの代わりにアンダースコアを使用します。

これは古いバージョンのpython 3でformat(num, ",d")を使うのと同じです。

47
Emil Stenström

これが1行の正規表現の置き換えです。

re.sub("(\d)(?=(\d{3})+(?!\d))", r"\1,", "%d" % val)

不正な出力に対してのみ機能します。

import re
val = 1234567890
re.sub("(\d)(?=(\d{3})+(?!\d))", r"\1,", "%d" % val)
# Returns: '1,234,567,890'

val = 1234567890.1234567890
# Returns: '1,234,567,890'

4桁未満の浮動小数点数の場合は、フォーマット指定子を%.3fに変更します。

re.sub("(\d)(?=(\d{3})+(?!\d))", r"\1,", "%.3f" % val)
# Returns: '1,234,567,890.123'

NB: 小数点以下の桁数をグループ化しようとするため、3桁を超える桁数では正しく機能しません。

re.sub("(\d)(?=(\d{3})+(?!\d))", r"\1,", "%.5f" % val)
# Returns: '1,234,567,890.12,346'

使い方

それを分解しましょう。

re.sub(pattern, repl, string)

pattern = \
    "(\d)           # Find one digit...
     (?=            # that is followed by...
         (\d{3})+   # one or more groups of three digits...
         (?!\d)     # which are not followed by any more digits.
     )",

repl = \
    r"\1,",         # Replace that one digit by itself, followed by a comma,
                    # and continue looking for more matches later in the string.
                    # (re.sub() replaces all matches it finds in the input)

string = \
    "%d" % val      # Format the string as a decimal to begin with
35
Daniel Fortunov

これは私がフロートのためにしていることです。正直なところ、私はそれがどのバージョンのために働くのかわからない - 私は2.7を使っている:

my_number = 4385893.382939491

my_string = '{:0,.2f}'.format(my_number)

戻り値:4,385,893.38

更新:私は最近このフォーマットで問題を抱えていましたが(正確な理由はわかりません)、0を削除することで解決できました:

my_string = '{:,.2f}'.format(my_number)
24
elPastor

ロケール表現に'{:n}'.format( value )を使うこともできます。これがロケールソリューションの最も簡単な方法だと思います。

詳細については、 Python DOCthousandsを検索してください。

通貨の場合は、フラグgroupingを設定して、locale.currencyを使用できます。

コード

import locale

locale.setlocale( locale.LC_ALL, '' )
locale.currency( 1234567.89, grouping = True )

出力

'Portuguese_Brazil.1252'
'R$ 1.234.567,89'
18
DiogoLR

これには標準のライブラリ関数が必要であると確信していますが、再帰を使用して自分で作成しようとするのは楽しかったので、思いついたのは次のとおりです。

def intToStringWithCommas(x):
    if type(x) is not int and type(x) is not long:
        raise TypeError("Not an integer!")
    if x < 0:
        return '-' + intToStringWithCommas(-x)
    Elif x < 1000:
        return str(x)
    else:
        return intToStringWithCommas(x / 1000) + ',' + '%03d' % (x % 1000)

そうは言っても、他の人がそれを行う標準的な方法を見つけた場合は、代わりにそれを使うべきです。

11
Mark Byers

コメント からactivestateレシピ 498181 私はこれを作り直しました:

import re
def thous(x, sep=',', dot='.'):
    num, _, frac = str(x).partition(dot)
    num = re.sub(r'(\d{3})(?=\d)', r'\1'+sep, num[::-1])[::-1]
    if frac:
        num += dot + frac
    return num

それは正規表現機能を使用します: lookahead すなわち(?=\d)は、それらの後に数字がある3つの数字のグループだけが確実にコンマを取得するようにします。この時点で文字列が逆になっているので、 'after'と言います。

[::-1]は文字列を逆にするだけです。

8
Tom Viner

Ian Schneiderの答えを少し拡張します。

カスタムの千単位の区切り記号を使用する場合、最も簡単な解決策は次のとおりです。

'{:,}'.format(value).replace(',', your_custom_thousands_separator)

'{:,.2f}'.format(123456789.012345).replace(',', ' ')

このようなドイツ語の表現が必要な場合は、もう少し複雑になります。

('{:,.2f}'.format(123456789.012345)
          .replace(',', ' ')  # 'save' the thousands separators 
          .replace('.', ',')  # dot to comma
          .replace(' ', '.')) # thousand separators to dot
8
Martin Thoma

Python 3

-

整数(10進数なし):

"{:,d}".format(1234567)

-

浮動小数点数(10進数):

"{:,.2f}".format(1234567)

ここでfの前の数字は小数点以下の桁数を指定します。

-

ボーナス

インドのlakhs/croresナンバリングシステム(12,34,567)のクイックアンドダーティスターター関数

https://stackoverflow.com/a/44832241/4928578

6
Adam Starrh

受け入れられた答えは大丈夫ですが、私は実際にformat(number,',')を好みます。私が解釈して覚えるのは簡単です。

https://docs.python.org/3/library/functions.html#format

5
Magenta Nova

pythonバージョン2.6からこれを行うことができます。

def format_builtin(n):
    return format(n, ',')

Pythonのバージョンが2.6未満の場合、そしてあなたの情報のためだけに、これは2つの手動の解決策です、それらは浮動小数点数を整数に変えますが、負の数は正しく働きます:

def format_number_using_lists(number):
    string = '%d' % number
    result_list = list(string)
    indexes = range(len(string))
    for index in indexes[::-3][1:]:
        if result_list[index] != '-':
            result_list.insert(index+1, ',')
    return ''.join(result_list)

ここで注意すべきことがいくつかあります。

  • この行: string = '%d'%number は数値を文字列に美しく変換し、負数をサポートし、浮動小数点から小数を削除して整数にします。
  • このスライス indexes [:: - 3] は最後から3番目の各項目を返すので、最後の項目を削除するために別のスライス [1:] を使用しました。最後の番号
  • この条件付き 負の数をサポートするためにl [index]!= ' - ' が使用されている場合は、マイナス記号の後にコンマを挿入しないでください。

そしてもっとハードコアなバージョン:

def format_number_using_generators_and_list_comprehensions(number):
    string = '%d' % number
    generator = reversed( 
        [
            value+',' if (index!=0 and value!='-' and index%3==0) else value
            for index,value in enumerate(reversed(string))
        ]
    )
    return ''.join(generator)
4
arka

私はPythonの初心者ですが、経験豊富なプログラマーです。私はPython 3.5を持っているので、カンマを使うことができますが、それでも興味深いプログラミング演習です。符号なし整数の場合を考えます。何千ものセパレータを追加するための最も読みやすいPythonプログラムは、次のようになります。

def add_commas(instr):
    out = [instr[0]]
    for i in range(1, len(instr)):
        if (len(instr) - i) % 3 == 0:
            out.append(',')
        out.append(instr[i])
    return ''.join(out)

リスト内包表記を使用することも可能です。

add_commas(instr):
    rng = reversed(range(1, len(instr) + (len(instr) - 1)//3 + 1))
    out = [',' if j%4 == 0 else instr[-(j - j//4)] for j in rng]
    return ''.join(out)

これはより短くて、そしてただのライナーであるかもしれません、しかしあなたはそれがなぜ働くか理解するために若干の精神体操をしなければならないでしょう。どちらの場合も、次のようになります。

for i in range(1, 11):
    instr = '1234567890'[:i]
    print(instr, add_commas(instr))
1 1
12 12
123 123
1234 1,234
12345 12,345
123456 123,456
1234567 1,234,567
12345678 12,345,678
123456789 123,456,789
1234567890 1,234,567,890

あなたがプログラムを理解させたいのであれば、最初のバージョンがより賢明な選択です。

1
Geoff Fergusson

Python 2.5以降とPython 3用のライナー(正の整数のみ):

''.join(reversed([x + (',' if i and not i % 3 else '') for i, x in enumerate(reversed(str(1234567)))]))
1
Collin Anderson

これはフロートに対しても有効なものです。

def float2comma(f):
    s = str(abs(f)) # Convert to a string
    decimalposition = s.find(".") # Look for decimal point
    if decimalposition == -1:
        decimalposition = len(s) # If no decimal, then just work from the end
    out = "" 
    for i in range(decimalposition+1, len(s)): # do the decimal
        if not (i-decimalposition-1) % 3 and i-decimalposition-1: out = out+","
        out = out+s[i]      
    if len(out):
        out = "."+out # add the decimal point if necessary
    for i in range(decimalposition-1,-1,-1): # working backwards from decimal point
        if not (decimalposition-i-1) % 3 and decimalposition-i-1: out = ","+out
        out = s[i]+out      
    if f < 0:
        out = "-"+out
    return out

使用例

>>> float2comma(10000.1111)
'10,000.111,1'
>>> float2comma(656565.122)
'656,565.122'
>>> float2comma(-656565.122)
'-656,565.122'
1
Edward van Kuik

イタリアの数字:千の区切り文字は '。'です。

私はこのようにしてそれを解決しました。

from random import randint

voci = {
    "immobilizzazioni": randint(200000, 500000),
    "tfr": randint(10000, 25000),
    "ac": randint(150000, 200000),
    "fondo": randint(10500, 22350),
    "debiti": randint(150000, 250000),
    "ratei_attivi": randint(2000, 2500),
    "ratei_passivi": randint(1500, 2600),
    "crediti_v_soci": randint(10000, 30000)
}


testo_rnd2 = """Nell’azienda Hypermax S.p.a. di Bologna le immobilizzazioni valgono {immobilizzazioni:,} €, i debiti per TFR sono pari a {tfr:,} €, l’attivo circolante è di {ac:,} euro, il fondo rischi ed oneri ha un importo pari a {fondo:,} euro, i debiti sono {debiti:,} €, i ratei e risconti attivi sono pari a {ratei_attivi:,} euro, i ratei e risconti passivi sono pari a {ratei_passivi:,} euro. I crediti verso i soci sono pari a {crediti_v_soci:,} euro."""

print(testo_rnd2)

アウト:ルイモリザザジオバルゴノ419.168€。 TFR sono pari 13.255€でデビット。 195.443ユーロです。 13.374ユーロの価格で販売されています。私debiti sono 180.947€私は料金をチェックしています。私はリスボンティpassivi sono pari 1.864ユーロを評価する。私は私と私のsoci sono pari 17.630ユーロのcrediti.

0
Giovanni Gianni

私はpython 2.5を使っているので、組み込みのフォーマットにアクセスすることはできません。

私はDjangoのコードintcomma(以下のコードではintcomma_recurs)を見て、それが再帰的であり、また毎回の正規表現をコンパイルするのも良いことではないので非効率であることに気づきました。 Djangoは実際にはこの種の低レベルのパフォーマンスに焦点を当てていないため、これは必ずしも「問題」ではありません。また、私はパフォーマンスの10倍の違いを期待していましたが、それはわずか3倍遅くなります。

好奇心から私はいくつかのバージョンのintcommaを実装して、regexを使用したときのパフォーマンス上の利点を確認しました。私のテストデータは、この作業には多少の利点がありますが、驚くほど多くはありません。

私はまた私が疑ったものを見ることができてうれしかった:逆xrangeアプローチを使用することは正規表現なしのケースでは不要であるが、それは〜10%のパフォーマンスの犠牲でコードをわずかに良く見せる。

また、私はあなたが渡しているものは文字列で、やや数字のように見えると思います。それ以外の結果は未定です。

from __future__ import with_statement
from contextlib import contextmanager
import re,time

re_first_num = re.compile(r"\d")
def intcomma_noregex(value):
    end_offset, start_digit, period = len(value),re_first_num.search(value).start(),value.rfind('.')
    if period == -1:
        period=end_offset
    segments,_from_index,leftover = [],0,(period-start_digit) % 3
    for _index in xrange(start_digit+3 if not leftover else start_digit+leftover,period,3):
        segments.append(value[_from_index:_index])
        _from_index=_index
    if not segments:
        return value
    segments.append(value[_from_index:])
    return ','.join(segments)

def intcomma_noregex_reversed(value):
    end_offset, start_digit, period = len(value),re_first_num.search(value).start(),value.rfind('.')
    if period == -1:
        period=end_offset
    _from_index,segments = end_offset,[]
    for _index in xrange(period-3,start_digit,-3):
        segments.append(value[_index:_from_index])
        _from_index=_index
    if not segments:
        return value
    segments.append(value[:_from_index])
    return ','.join(reversed(segments))

re_3digits = re.compile(r'(?<=\d)\d{3}(?!\d)')
def intcomma(value):
    segments,last_endoffset=[],len(value)
    while last_endoffset > 3:
        digit_group = re_3digits.search(value,0,last_endoffset)
        if not digit_group:
            break
        segments.append(value[digit_group.start():last_endoffset])
        last_endoffset=digit_group.start()
    if not segments:
        return value
    if last_endoffset:
        segments.append(value[:last_endoffset])
    return ','.join(reversed(segments))

def intcomma_recurs(value):
    """
    Converts an integer to a string containing commas every three digits.
    For example, 3000 becomes '3,000' and 45000 becomes '45,000'.
    """
    new = re.sub("^(-?\d+)(\d{3})", '\g<1>,\g<2>', str(value))
    if value == new:
        return new
    else:
        return intcomma(new)

@contextmanager
def timed(save_time_func):
    begin=time.time()
    try:
        yield
    finally:
        save_time_func(time.time()-begin)

def testset_xsimple(func):
    func('5')

def testset_simple(func):
    func('567')

def testset_onecomma(func):
    func('567890')

def testset_complex(func):
    func('-1234567.024')

def testset_average(func):
    func('-1234567.024')
    func('567')
    func('5674')

if __== '__main__':
    print 'Test results:'
    for test_data in ('5','567','1234','1234.56','-253892.045'):
        for func in (intcomma,intcomma_noregex,intcomma_noregex_reversed,intcomma_recurs):
            print func.__name__,test_data,func(test_data)
    times=[]
    def overhead(x):
        pass
    for test_run in xrange(1,4):
        for func in (intcomma,intcomma_noregex,intcomma_noregex_reversed,intcomma_recurs,overhead):
            for testset in (testset_xsimple,testset_simple,testset_onecomma,testset_complex,testset_average):
                for x in xrange(1000): # prime the test
                    testset(func)
                with timed(lambda x:times.append(((test_run,func,testset),x))):
                    for x in xrange(50000):
                        testset(func)
    for (test_run,func,testset),_delta in times:
        print test_run,func.__name__,testset.__name__,_delta

そして、これがテスト結果です。

intcomma 5 5
intcomma_noregex 5 5
intcomma_noregex_reversed 5 5
intcomma_recurs 5 5
intcomma 567 567
intcomma_noregex 567 567
intcomma_noregex_reversed 567 567
intcomma_recurs 567 567
intcomma 1234 1,234
intcomma_noregex 1234 1,234
intcomma_noregex_reversed 1234 1,234
intcomma_recurs 1234 1,234
intcomma 1234.56 1,234.56
intcomma_noregex 1234.56 1,234.56
intcomma_noregex_reversed 1234.56 1,234.56
intcomma_recurs 1234.56 1,234.56
intcomma -253892.045 -253,892.045
intcomma_noregex -253892.045 -253,892.045
intcomma_noregex_reversed -253892.045 -253,892.045
intcomma_recurs -253892.045 -253,892.045
1 intcomma testset_xsimple 0.0410001277924
1 intcomma testset_simple 0.0369999408722
1 intcomma testset_onecomma 0.213000059128
1 intcomma testset_complex 0.296000003815
1 intcomma testset_average 0.503000020981
1 intcomma_noregex testset_xsimple 0.134000062943
1 intcomma_noregex testset_simple 0.134999990463
1 intcomma_noregex testset_onecomma 0.190999984741
1 intcomma_noregex testset_complex 0.209000110626
1 intcomma_noregex testset_average 0.513000011444
1 intcomma_noregex_reversed testset_xsimple 0.124000072479
1 intcomma_noregex_reversed testset_simple 0.12700009346
1 intcomma_noregex_reversed testset_onecomma 0.230000019073
1 intcomma_noregex_reversed testset_complex 0.236999988556
1 intcomma_noregex_reversed testset_average 0.56299996376
1 intcomma_recurs testset_xsimple 0.348000049591
1 intcomma_recurs testset_simple 0.34600019455
1 intcomma_recurs testset_onecomma 0.625
1 intcomma_recurs testset_complex 0.773999929428
1 intcomma_recurs testset_average 1.6890001297
1 overhead testset_xsimple 0.0179998874664
1 overhead testset_simple 0.0190000534058
1 overhead testset_onecomma 0.0190000534058
1 overhead testset_complex 0.0190000534058
1 overhead testset_average 0.0309998989105
2 intcomma testset_xsimple 0.0360000133514
2 intcomma testset_simple 0.0369999408722
2 intcomma testset_onecomma 0.207999944687
2 intcomma testset_complex 0.302000045776
2 intcomma testset_average 0.523000001907
2 intcomma_noregex testset_xsimple 0.139999866486
2 intcomma_noregex testset_simple 0.141000032425
2 intcomma_noregex testset_onecomma 0.203999996185
2 intcomma_noregex testset_complex 0.200999975204
2 intcomma_noregex testset_average 0.523000001907
2 intcomma_noregex_reversed testset_xsimple 0.130000114441
2 intcomma_noregex_reversed testset_simple 0.129999876022
2 intcomma_noregex_reversed testset_onecomma 0.236000061035
2 intcomma_noregex_reversed testset_complex 0.241999864578
2 intcomma_noregex_reversed testset_average 0.582999944687
2 intcomma_recurs testset_xsimple 0.351000070572
2 intcomma_recurs testset_simple 0.352999925613
2 intcomma_recurs testset_onecomma 0.648999929428
2 intcomma_recurs testset_complex 0.808000087738
2 intcomma_recurs testset_average 1.81900000572
2 overhead testset_xsimple 0.0189998149872
2 overhead testset_simple 0.0189998149872
2 overhead testset_onecomma 0.0190000534058
2 overhead testset_complex 0.0179998874664
2 overhead testset_average 0.0299999713898
3 intcomma testset_xsimple 0.0360000133514
3 intcomma testset_simple 0.0360000133514
3 intcomma testset_onecomma 0.210000038147
3 intcomma testset_complex 0.305999994278
3 intcomma testset_average 0.493000030518
3 intcomma_noregex testset_xsimple 0.131999969482
3 intcomma_noregex testset_simple 0.136000156403
3 intcomma_noregex testset_onecomma 0.192999839783
3 intcomma_noregex testset_complex 0.202000141144
3 intcomma_noregex testset_average 0.509999990463
3 intcomma_noregex_reversed testset_xsimple 0.125999927521
3 intcomma_noregex_reversed testset_simple 0.126999855042
3 intcomma_noregex_reversed testset_onecomma 0.235999822617
3 intcomma_noregex_reversed testset_complex 0.243000030518
3 intcomma_noregex_reversed testset_average 0.56200003624
3 intcomma_recurs testset_xsimple 0.337000131607
3 intcomma_recurs testset_simple 0.342000007629
3 intcomma_recurs testset_onecomma 0.609999895096
3 intcomma_recurs testset_complex 0.75
3 intcomma_recurs testset_average 1.68300008774
3 overhead testset_xsimple 0.0189998149872
3 overhead testset_simple 0.018000125885
3 overhead testset_onecomma 0.018000125885
3 overhead testset_complex 0.0179998874664
3 overhead testset_average 0.0299999713898
0
parity3

これはPEPごとにpythonに焼き付けられています - > https://www.python.org/dev/peps/pep-0378/

千の区切り記号を持つ整数を表示するには、format(1000、 '、d')を使用するだけです。

pEPにはもっと多くのフォーマットが記述されています。

0

私はこのコードのpython 2とpython 3バージョンを持っています。私は質問がpython 2を求められたことを知っていますが、今(8年後に笑)人々はおそらくpython 3を使用するでしょう。

Python 3コード:

import random
number = str(random.randint(1, 10000000))
comma_placement = 4
print('The original number is: {}. '.format(number))
while True:
    if len(number) % 3 == 0:
        for i in range(0, len(number) // 3 - 1):
            number = number[0:len(number) - comma_placement + 1] + ',' + number[len(number) - comma_placement + 1:]
            comma_placement = comma_placement + 4
    else:
        for i in range(0, len(number) // 3):
            number = number[0:len(number) - comma_placement + 1] + ',' + number[len(number) - comma_placement + 1:]
    break
print('The new and improved number is: {}'.format(number))        


Python 2コード:(編集。Python2コードは機能しません。構文が異なると思います)。

import random
number = str(random.randint(1, 10000000))
comma_placement = 4
print 'The original number is: %s.' % (number)
while True:
    if len(number) % 3 == 0:
        for i in range(0, len(number) // 3 - 1):
            number = number[0:len(number) - comma_placement + 1] + ',' + number[len(number) - comma_placement + 1:]
            comma_placement = comma_placement + 4
    else:
        for i in range(0, len(number) // 3):
            number = number[0:len(number) - comma_placement + 1] + ',' + number[len(number) - comma_placement + 1:]
    break
print 'The new and improved number is: %s.' % (number) 
0
Dinosaur212

long(またはfloat、またはその他)をサブクラス化するだけです。これは非常に実用的です、なぜならこの方法であなたはまだあなたの数字を数学演算(そしてそれ故に既存のコード)の中で使うことができるけれども、それらはあなたの端末でうまく印刷されるでしょう。

>>> class number(long):

        def __init__(self, value):
            self = value

        def __repr__(self):
            s = str(self)
            l = [x for x in s if x in '1234567890']
            for x in reversed(range(len(s)-1)[::3]):
                l.insert(-x, ',')
            l = ''.join(l[1:])
            return ('-'+l if self < 0 else l) 

>>> number(-100000)
-100,000
>>> number(-100)
-100
>>> number(-12345)
-12,345
>>> number(928374)
928,374
>>> 345
0
twneale

これはコンマと一緒にお金をします

def format_money(money, presym='$', postsym=''):
    fmt = '%0.2f' % money
    dot = string.find(fmt, '.')
    ret = []
    if money < 0 :
        ret.append('(')
        p0 = 1
    else :
        p0 = 0
    ret.append(presym)
    p1 = (dot-p0) % 3 + p0
    while True :
        ret.append(fmt[p0:p1])
        if p1 == dot : break
        ret.append(',')
        p0 = p1
        p1 += 3
    ret.append(fmt[dot:])   # decimals
    ret.append(postsym)
    if money < 0 : ret.append(')')
    return ''.join(ret)
0
Bob F