473,383 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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

A.M
Hi,

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

Thank you,
Alan

Jun 1 '06 #1
9 3374
On 2/06/2006 9:08 AM, A.M wrote:
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
Jun 2 '06 #2

A.M wrote:
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??

Jun 2 '06 #3
A.M wrote:
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:
import locale
locale.setlocale(locale.LC_ALL, '') 'English_United States.1252' locale.format('%d', 123456789, True)

'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

Jun 2 '06 #4

A.M wrote:
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][i]
else:
yield ","
yield pieces[0][i]
if len(pieces) > 1:
yield "." + pieces[1]
jtm

Jun 2 '06 #5
John Machin wrote:
A.M wrote:
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??


If it's not a typo, it's probably a regional thing. See, e.g.,
http://en.wikipedia.org/wiki/Indian_numbering_system

--Ben

Jun 2 '06 #6

ja*************@gmail.com wrote:
A.M wrote:
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][i]
else:
yield ","
yield pieces[0][i]
if len(pieces) > 1:
yield "." + pieces[1]


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

Jun 2 '06 #7
A.M


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

Alan


Jun 2 '06 #8
D H
A.M wrote:
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/str...ing-in-csharp/
http://java.sun.com/docs/books/tutor...malFormat.html
Jun 2 '06 #9
John Machin <sj******@lexicon.net> wrote:
On 2/06/2006 9:08 AM, A.M wrote:
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

--
Warren Block * Rapid City, South Dakota * USA
Jun 3 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jhy-Chun Wang | last post by:
Hi, I have a c++ extension that does thing like: extern "C" static PyObject * nasUtil_access_cmd(PyObject *self, PyObject *args) { uint64_t paddr; uint64_t data;
19
by: rbt | last post by:
Here's the scenario: You have many hundred gigabytes of data... possible even a terabyte or two. Within this data, you have private, sensitive information (US social security numbers) about your...
3
by: stevek | last post by:
How do I format an integer. Add commas. 1234565 1,234,565 TIA
96
by: Gustav Hållberg | last post by:
I tried finding a discussion around adding the possibility to have optional underscores inside numbers in Python. This is a popular option available in several "competing" scripting langauges, that...
15
by: Claudio Grondi | last post by:
Let's consider a test source code given at the very end of this posting. The question is if Python allows somehow access to the bytes of the representation of a long integer or integer in...
0
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 349 open ( +7) / 3737 closed (+25) / 4086 total (+32) Bugs : 939 open (-12) / 6648 closed (+60) / 7587 total (+48) RFE : 249 open...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.