Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.044 X-Spam-Evidence: '*H*': 0.91; '*S*': 0.00; 'python': 0.08; '>>>>': 0.09; 'locale': 0.09; 'url:peps': 0.09; 'comma': 0.16; 'pythonic': 0.16; 'true)': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.18; 'cheers,': 0.20; 'cc:no real name:2**0': 0.21; 'header:In-Reply- To:1': 0.22; 'feb': 0.22; 'received:209.85.212.46': 0.23; 'received:mail-vw0-f46.google.com': 0.23; 'pep': 0.23; 'url:dev': 0.23; 'cc:2**0': 0.26; 'import': 0.27; "i'm": 0.28; 'message- id:@mail.gmail.com': 0.29; "skip:' 10": 0.29; 'print': 0.29; 'cc:addr:python.org': 0.29; 'pm,': 0.29; 'subject:number': 0.30; 'chris': 0.30; 'skip:l 30': 0.32; 'thu,': 0.32; 'received:209.85.212': 0.33; 'this.': 0.33; 'latter': 0.34; 'skip:" 20': 0.35; 'url:python': 0.35; 'received:google.com': 0.37; 'skip:" 10': 0.37; 'subject:with': 0.37; 'received:209.85': 0.38; 'some': 0.38; 'format': 0.38; 'url:org': 0.39; 'received:209': 0.39; 'quick': 0.61; 'more': 0.61; '12:39': 0.84; '378': 0.84; 'otten': 0.84; 'perpetual': 0.84; 'proper,': 0.84; 'sender:addr:chris': 0.84; 'to:none': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:cc:content-type; bh=jqAwK1zJ2nfciLnd7+W5w54UdBEcIjfdXrn064rG6tg=; b=Mvl/Cbs/omsMKJMfp0qrq1Wwh3y2WTch8e5DAIiH21Ocb3Hu63hzY1Brs1tB01cFEi I22DSr14F2ooVUrfYaL8iZWkcxjyEhF5xkwa70aOGgGPE9Ild8rDVE8P8bANiBxwlQFL egW3FJJwGATVa2smCP0s+tK6jBeWJrvrSPZk0= MIME-Version: 1.0 Sender: chris@rebertia.com In-Reply-To: References: Date: Thu, 9 Feb 2012 12:51:58 -0800 X-Google-Sender-Auth: 0IVCc0y9bxOtdRlbXqXGPke_gzc Subject: Re: Formate a number with commas From: Chris Rebert Cc: python-list@python.org Content-Type: text/plain; charset=UTF-8 X-Gm-Message-State: ALoCoQndi5Hk5beUeMpZo+GsFgWTNtXZ8DPBwB0lpv+BcOtP8YZhn0gvg9B/Qm2BBx4QyBjrK9Dj X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 29 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1328820721 news.xs4all.nl 6969 [2001:888:2000:d::a6]:39533 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:20109 On Thu, Feb 9, 2012 at 12:39 PM, Peter Otten <__peter__@web.de> wrote: > noydb wrote: > >> How do you format a number to print with commas? >> >> Some quick searching, i came up with: >> >>>>> import locale >>>>> locale.setlocale(locale.LC_ALL, "") >>>>> locale.format('%d', 2348721, True) >> '2,348,721' >> >> >> I'm a perpetual novice, so just looking for better, slicker, more >> proper, pythonic ways to do this. > >>>> import locale >>>> locale.setlocale(locale.LC_ALL, "") > 'de_DE.UTF-8' >>>> "{:n}".format(1234) # locale-aware > '1.234' >>>> "{:,d}".format(1234) # always a comma > '1,234' The latter requires Python 3.1+ and is courtesy PEP 378 (http://www.python.org/dev/peps/pep-0378/ ). Cheers, Chris