formatting numbers for output

Timothy Grant tjg at exceptionalminds.com
Sun Feb 18 23:14:33 CET 2001


This is something you have to do yourself. I've attached a little module
I wrote to do it. It's not too flexible, it only deals with the US format,
but it works pretty well. I created it for a Tkinter app that needed to 
display pretty numbers, so it actually goes both ways unformatted to
formatted and formatted to unformatted.

On Sun, Feb 18, 2001 at 03:43:01PM +0100, Fernando Rodríguez wrote:
> Hi!
> 
> 	How can I format a number so it pretty prints:
> 
> 15,456.6 instead of 15456.6 ?
> 

-- 
Stand Fast,
    tjg.

Timothy Grant                         tjg at exceptionalminds.com
Red Hat Certified Engineer            www.exceptionalminds.com
Avalon Technology Group, Inc.                   (503) 246-3630
>>>>>>>>>>>>>Linux, because rebooting is *NOT* normal<<<<<<<<<
>>>>This machine was last rebooted:  33 days  2:23 hours ago<<
-------------- next part --------------
#! /usr/bin/env python
#
import string

############################################################
#
# Name: commanumber()
#
# Purpose:  To format a number with commas and possibly a
#           dollar sign.
#
# Arguments:    n  = the number to be converted
#               dp = number of decimal places (defaults to 2)
#               ds = dollar sign (1|0) (defaults to $)
#  
def commanumber(n, dp=2, ds=1):
    
    if not n:
        return ''       # If None then bail out here
        
    if type(n) == type('x'):
        if n == '':
            return ''   # If an empty string then bail out here.
        n = string.atof(n)
    
    m = '%0.*f' % (dp, n)
    
    d = string.split(m, '.')    # Split at the decimal point
    r = list(d[0])
    
    # It looks ugly but it really isn't. A couple of really nice Pythonisms
    # make it work right. The first is list insertion, and the second is integer
    # division.
    #
    # the insertion point is calculated based on the counter item, plus the a left
    # shift for each ',' already inserted the ((x/3)-1)
    #
    for x in range(3, len(r), 3):
        r[(x+((x/3)-1))*(-1):(x+((x/3)-1))*(-1)] = [',']
    
    if ds:
        s = '$'
    else:
        s = ''          # Rebuild the string from the list
        
    for i in r:
        s = s + i
        
    if len(d) == 2: # Check to see if we have a decimal portion to add
        return s + '.' + d[1]
    else:
        return s
    
    
############################################################
#
# Name: stripfmt()
#
# Purpose:  Strip all formatting from a prettified number
#
# Arguments:    n = the number to strip
#  
def stripfmt(n):
    n = string.replace(n, ',', '')  #remove commas
    n = string.replace(n, '$', '')  #remove dollar signs.
    return n
    
    
if __name__ == '__main__':
    test = 12345.346
    
    print 'number           -->       ' + `test`
    print 'commanumber(test)-->       ' + commanumber(test)
    print 'commanumber(test,1)-->     ' + commanumber(test,1)
    print 'commanumber(test,2)-->     ' + commanumber(test,2)
    print 'commanumber(test,3)-->     ' + commanumber(test,3)
    print 'commanumber(test,4)-->     ' + commanumber(test,4)


More information about the Python-list mailing list