Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #20119

Re: Formate a number with commas

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <jjposner@optimum.net>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.011
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'python': 0.08; 'header:In- reply-to:1': 0.09; 'output': 0.10; '-john': 0.16; '1234': 0.16; 'expression,': 0.16; 'received:167.206.4': 0.16; 'received:cv.net': 0.16; 'received:hcvlny.cv.net': 0.16; 'received:srv.hcvlny.cv.net': 0.16; 'wrote:': 0.18; 'to:2**1': 0.21; 'module': 0.26; 'import': 0.27; 'print': 0.29; 'pm,': 0.29; 'admit': 0.30; 'preferable': 0.30; 'subject:number': 0.30; 'certainly': 0.32; 'header:User-Agent:1': 0.33; 'to:addr:python- list': 0.35; 'regular': 0.35; 'to:name:python-list': 0.37; 'subject:with': 0.37; 'received:192': 0.38; 'format': 0.38; 'to:addr:python.org': 0.40; 'skip:1 10': 0.63; 'readily': 0.93
Date Thu, 09 Feb 2012 17:57:42 -0500
From John Posner <jjposner@optimum.net>
Subject Re: Formate a number with commas
In-reply-to <eb1973ef-913e-408a-b491-d1449c8434e2@c21g2000yqi.googlegroups.com>
To noydb <jenn.duerr@gmail.com>, python-list <python-list@python.org>
MIME-version 1.0
Content-type text/plain; charset=ISO-8859-1
Content-transfer-encoding 7BIT
X-Enigmail-Version 1.3.5
References <eb1973ef-913e-408a-b491-d1449c8434e2@c21g2000yqi.googlegroups.com>
User-Agent Mozilla/5.0 (Windows NT 5.1; rv:9.0) Gecko/20111222 Thunderbird/9.0.1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.5620.1328830071.27778.python-list@python.org> (permalink)
Lines 28
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1328830071 news.xs4all.nl 6974 [2001:888:2000:d::a6]:40288
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:20119

Show key headers only | View raw


On 2:59 PM, noydb wrote:
> How do you format a number to print with commas?

I would readily admit that both the "locale" module and "format" method
are preferable to this regular expression, which certainly violates the
"readability counts" dictum:

    r"(?<=\d)(?=(\d\d\d)+$)"

# python 2.6.6
import re
find_blocks = re.compile(r"(?<=\d)(?=(\d\d\d)+$)")
for numstr in "1 12 123 1234 12345 123456 1234567 12345678".split():
    print find_blocks.sub("," , numstr)

output is:

1
12
123
1,234
12,345
123,456
1,234,567
12,345,678

-John

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Formate a number with commas noydb <jenn.duerr@gmail.com> - 2012-02-09 12:08 -0800
  Re: Formate a number with commas Neil Cerutti <neilc@norwich.edu> - 2012-02-09 20:17 +0000
  Re: Formate a number with commas Peter Otten <__peter__@web.de> - 2012-02-09 21:39 +0100
  Re: Formate a number with commas Chris Rebert <clp2@rebertia.com> - 2012-02-09 12:51 -0800
  Re: Formate a number with commas Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2012-02-09 22:13 +0100
  Re: Formate a number with commas Peter Otten <__peter__@web.de> - 2012-02-09 22:16 +0100
  Re: Formate a number with commas Chris Rebert <clp2@rebertia.com> - 2012-02-09 14:12 -0800
  Re: Formate a number with commas John Posner <jjposner@optimum.net> - 2012-02-09 17:57 -0500
  Re: Formate a number with commas Terry Reedy <tjreedy@udel.edu> - 2012-02-09 22:06 -0500

csiph-web