Can Python format long integer 123456789 to 12,3456,789 ?

A

A.M

Hi,

Is there any built in feature in Python that can format long integer
123456789 to 12,3456,789 ?

Thank you,
Alan
 
J

John Machin

Hi,

Is there any built in feature in Python that can format long integer
123456789 to 12,3456,789 ?

Thank you,
Alan

Not that I know of, but this little kludge may help:
8<---
import re

subber = re.compile(r'^(-?\d+)(\d{3})').sub

def fmt_thousands(amt, sep):
if amt in ('', '-'):
return ''
repl = r'\1' + sep + r'\2'
while True:
new_amt = subber(repl, amt)
if new_amt == amt:
return amt
amt = new_amt

if __name__ == "__main__":
for prefix in ['', '-']:
for k in range(11):
arg = prefix + "1234567890.1234"[k:]
print "<%s> <%s>" % (arg, fmt_thousands(arg, ","))
8<---

HTH,
John
 
J

John Machin

A.M said:
Hi,

Is there any built in feature in Python that can format long integer
123456789 to 12,3456,789 ?

Sorry about my previous post. It would produce 123,456,789.
"12,3456,789" is weird -- whose idea PHB or yours??
 
B

Ben Cartwright

A.M said:
Is there any built in feature in Python that can format long integer
123456789 to 12,3456,789 ?

The locale module can help you here:
'123,456,789'

Be sure to read the caveats for setlocale in the module docs:
http://docs.python.org/lib/node323.html
I'd recommend calling setlocale only once, and always at the start of
your program.

--Ben
 
J

james.wondrasek

A.M said:
Hi,

Is there any built in feature in Python that can format long integer
123456789 to 12,3456,789 ?

Thank you,
Alan

I did this for putting commas into monetary amounts (thus the .2f):

def commas(value):
return "".join(commafy("%.2f" % value))

def commafy(s):
pieces = s.split(".")
l = len(pieces[0])
for i in range(0,l):
if (l - i) % 3 or not i:
yield pieces[0]
else:
yield ","
yield pieces[0]
if len(pieces) > 1:
yield "." + pieces[1]


jtm
 
J

John Machin

A.M said:
Hi,

Is there any built in feature in Python that can format long integer
123456789 to 12,3456,789 ?

Thank you,
Alan

I did this for putting commas into monetary amounts (thus the .2f):

def commas(value):
return "".join(commafy("%.2f" % value))

def commafy(s):
pieces = s.split(".")
l = len(pieces[0])
for i in range(0,l):
if (l - i) % 3 or not i:
yield pieces[0]
else:
yield ","
yield pieces[0]
if len(pieces) > 1:
yield "." + pieces[1]


I dips me lid -- that's far fuglier than mine!!!
 
A

A.M

Thanks for help. Is there any comprehensive library for number formatting
available on the net?



Alan
 
D

D H

A.M said:
Hi,

Is there any built in feature in Python that can format long integer
123456789 to 12,3456,789 ?

Apparently you need to use the locale module. This is the
example they give online to print a simple number with commas:

import locale
locale.setlocale(locale.LC_ALL, 'English_United States.1252')
print locale.format("%d", 123456789, grouping=True)

Someone should make a module that uses .NET or Java's formmating.
You just use {0:n} or ###,### instead of all the above.
http://blog.stevex.net/index.php/string-formatting-in-csharp/
http://java.sun.com/docs/books/tutorial/i18n/format/decimalFormat.html
 
W

Warren Block

John Machin said:
Hi,

Is there any built in feature in Python that can format long integer
123456789 to 12,3456,789 ?

Thank you,
Alan

Not that I know of, but this little kludge may help:
8<---
import re

subber = re.compile(r'^(-?\d+)(\d{3})').sub

def fmt_thousands(amt, sep):
if amt in ('', '-'):
return ''
repl = r'\1' + sep + r'\2'
while True:
new_amt = subber(repl, amt)
if new_amt == amt:
return amt
amt = new_amt

if __name__ == "__main__":
for prefix in ['', '-']:
for k in range(11):
arg = prefix + "1234567890.1234"[k:]
print "<%s> <%s>" % (arg, fmt_thousands(arg, ","))
8<---

Why not just port the Perl "commify" code? You're close to it, at least
for the regex:

# From perldoc perlfaq5
# 1 while s/^([-+]?\d+)(\d{3})/$1,$2/;

# Python version

import re

def commify(n):
while True:
(n, count) = re.subn(r'^([-+]?\d+)(\d{3})', r'\1,\2', n)
if count == 0: break
return n
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,297
Messages
2,571,536
Members
48,283
Latest member
SherriP988

Latest Threads

Top