473,379 Members | 1,278 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,379 software developers and data experts.

format a number for output

if i have a number, say the size of a file, is there an easy way to
output it so that it includes commas?

for example:

1890284

would be:

1,890,284

I am looking for something builtin to python, not a third party lib.

thanks

Aug 7 '06 #1
4 1671
In <11**********************@i3g2000cwc.googlegroups. com>, abcd wrote:
if i have a number, say the size of a file, is there an easy way to
output it so that it includes commas?

for example:

1890284

would be:

1,890,284
I think this comes close:

In [23]: import locale

In [24]: locale.setlocale(locale.LC_ALL, '')
Out[24]: 'en_US.UTF-8'

In [25]: locale.format('%d', 1890284, True)
Out[25]: '1,890,284'

It's not always commas but whatever character is used to group thousands
in the active locale.

Ciao,
Marc 'BlackJack' Rintsch
Aug 7 '06 #2
abcd wrote:
if i have a number, say the size of a file, is there an easy way to
output it so that it includes commas?

for example:

1890284

would be:

1,890,284
see also this thread:

http://tinyurl.com/qf6ew

rd

Aug 7 '06 #3
"abcd" <co*******@gmail.comwrites:
1890284

would be:

1,890,284
"To iterate is human; to recurse, divine":

def commafy(n):
if n < 0: return '-' + commafy(-n)
if n >= 1000: return '%s,%03d' % (commafy(n//1000), n % 1000)
return '%s'% n

I don't like the locale solution because of how messy locales are.
Aug 7 '06 #4

Paul Rubin wrote:
"To iterate is human; to recurse, divine":

def commafy(n):
if n < 0: return '-' + commafy(-n)
if n >= 1000: return '%s,%03d' % (commafy(n//1000), n % 1000)
return '%s'% n

I don't like the locale solution because of how messy locales are.
That's a keeper!

Thanks!

rd

Aug 7 '06 #5

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

Similar topics

1
by: Nick | last post by:
Well, the project I am working on has now come to a screeching halt! I have been developing a program that heavily utilizes ADO.NET record sets. To generate reports, I convert the recordset to XML,...
6
by: Stuart McGraw | last post by:
I am looking for a VBA "format" or "template" function, that is, a function that takes a format string and a varying number of arguments, and substitutes the argument values into the format string...
7
by: Sobhan | last post by:
Hi all, Iam writing a small C program to print a array of numbers in Comma Separated Values in VC++ editor. Lets say: Title Title1 Title2 ----- ------ ------ 10,20,30 50,90,100 120,180,300
23
by: Matt Garman | last post by:
Is there a clean, portable way to determine the maximum value of converted numerical fields with printf()-like functions? Doing this at compile-time would be preferable. For example, %i should...
3
by: Boz | last post by:
Hi, I am trying to use string.Format() to output the value of a double. double da = 100000000000.99994; double db = 100000000000.9994; double dc = 100000000000.994; double dd =...
2
by: Pat | last post by:
Here is my problem that I am having with a current project. I went and backed up all my hard drives to DVD's. After backing up the drives, I used a batch file to get all the file names from the...
2
by: Kannan | last post by:
In section 7.19.6.1 on ISO C specification, it says about a format character "space". When and how we will use this "space" format? I mean is there a usage, for e.g. like printf("%space",...); ? ...
5
by: Gary Wessle | last post by:
Hi I am trying to pretty print to file 3 vectors (int,double,double) where all the cells match like a table format where every thing lines up to the left and space padded to the right. does c++...
7
by: basyarie | last post by:
Dear VB mania, especially VB6 specialist I have a problem with my GPS. So far, I have a GPS-M1zz from Pioneer Navicom company. It has 2 type of data format, i.e. Pioneer Format and NMEA data...
18
by: Dirk Hagemann | last post by:
Hello, From a zone-file of a Microsoft Active Directory integrated DNS server I get the date/time of the dynamic update entries in a format, which is as far as I know the hours since january 1st...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.