Formatting a numerical string w/commas

Berthold Höllmann bhoel at server.python.net
Wed Feb 23 09:30:10 CET 2000


Timothy Grant <tjg at avalongroup.net> writes:

> I've seen some traffic on this formatting numbers, and recently had to
> format and deformat some number for a Tkinter app I was writing, so I
> wrote to fairly generic functions for doing so.
> 
> This is the first time I've ever posted *real* code on the list so
> please be gentle, but constructive criticism, especially about style, is
> always welcome.

Maybe you also like to take a look at my "UnitPrint.py". It is hidden
in my PyLaTeX module on starship
(http://starship.python.net/crew/bhoel/). It is primaly written to
print PhysicalUnits from Konrad Hinsens ScientificPython. Here's a
sample session (using german locale settings):

    >>> from Scientific.Physics import PhysicalQuantities
    >>> p = PhysicalQuantities.PhysicalQuantity
    >>> import UnitPrint
    >>> from UnitPrint import *
    >>> print "got: ->%s<-" % formatInt(1)
    got: ->1<-
    >>> print "got: ->%s<-" % formatInt(12)
    got: ->12<-
    >>> print "got: ->%s<-" % formatInt(123)
    got: ->123<-
    >>> print "got: ->%s<-" % formatInt(1234)
    got: ->1234<-
    >>> print "got: ->%s<-" % formatInt(12345)
    got: ->12345<-
    >>> print "got: ->%s<-" % formatInt(123456)
    got: ->123456<-
    >>> print "got: ->%s<-" % formatInt(1234567)
    got: ->1234567<-
    >>> print "got: ->%s<-" % formatInt(12345678)
    got: ->12345678<-
    >>> print "got: ->%s<-" % formatInt(123456789)
    got: ->123456789<-
    >>> UnitPrint.thousand_sep = r"\,"
    >>> print "got: ->%s<-" % formatInt(1234567890)
    got: ->1\,234\,567\,890<-
    >>> UnitPrint.thousand_sep = r""
    >>> print "got: ->%s<-" % formatInt(1234567890,
    ...                                 {'thousand_sep': '-'})
    got: ->1-234-567-890<-
    >>> print "got: ->%s<-" % formatInt(1.)
    got: ->1<-
    >>> print "got: ->%s<-" % formatInt(12.)
    got: ->12<-
    >>> print "got: ->%s<-" % formatInt(123.)
    got: ->123<-
    >>> print "got: ->%s<-" % formatInt(1234.)
    got: ->1234<-
    >>> print "got: ->%s<-" % formatInt(12345.)
    got: ->12345<-
    >>> print "got: ->%s<-" % formatInt(123456.)
    got: ->123456<-
    >>> print "got: ->%s<-" % formatInt(1234567.)
    got: ->1234567<-
    >>> print "got: ->%s<-" % formatInt(12345678.)
    got: ->12345678<-
    >>> print "got: ->%s<-" % formatInt(123456789.)
    got: ->123456789<-
    >>> print "got: ->%s<-" % formatInt(1234567890.)
    got: ->1234567890<-
    >>> value = 1234.567891234
    >>> print "got: ->%s<-" % formatFloat(value, 1)
    got: ->1234.6<-
    >>> print "got: ->%s<-" % formatFloat(value, format=2)
    got: ->1234.57<-
    >>> print "got: ->%s<-" % formatFloat(value, 3)
    got: ->1234.568<-
    >>> print "got: ->%s<-" % formatFloat(value, 4)
    got: ->1234.5679<-
    >>> print "got: ->%s<-" % formatFloat(value, 5)
    got: ->1234.56789<-
    >>> print "got: ->%s<-" % formatFloat(value, 6)
    got: ->1234.567891<-
    >>> UnitPrint.thousand_sep = r"\,"
    >>> print "got: ->%s<-" % UnitPrint.formatFloat(value, 7)
    got: ->1\,234.567\,891\,2<-
    >>> print "got: ->%s<-" % formatFloat(value, 7)
    got: ->1\,234.567\,891\,2<-
    >>> print "got: ->%s<-" % formatFloat(value, 8,
    ...                                   {'thousand_sep': '-',
    ...                                    'decimal_point': '#'})
    got: ->1-234#567-891-23<-
    >>> print generateUnitList(1)
    ('1.0',)
    >>> print generateUnitList(p("1 m/m"))
    ('1.0',)
    >>> print generateUnitList(1, formatInt)
    ('1',)
    >>> print generateUnitList(1, formatFloat)
    ('1.000',)
    >>> print generateUnitList(p("1m"))
    ('1.0', 'm')
    >>> print generateUnitList(p("1m/s"))
    ('1.0', 'm', 's')
    >>> print generateUnitList(p("1kg*m/s**2"))
    ('1.0', 'kgm', 's2')
    >>> print generateUnitList(1/p("1kg*m/s**2"))
    ('1.0', 's2', 'mkg')
    >>> print generateUnitList(p(1, "1/kg/m/s**2"))
    ('1.0', '1', 'kgms2')
    >>> print generateUnitList(p("1m"), formatInt)
    ('1', 'm')
    >>> print generateUnitList(p("1m"), formatFloat, 2)
    ('1.00', 'm')
    >>> print generateUnitList(p("1m"), formatFloat, 3)
    ('1.000', 'm')
    >>> print generateUnitList(p("12345.678901m"), formatFloat, 5)
    ('12\\,345.678\\,90', 'm')
    >>> print generateUnitList(p("1m/s"), formatInt)
    ('1', 'm', 's')
    >>> print generateUnitList(p("1kg*m/s**2"), formatInt)
    ('1', 'kgm', 's2')
    >>> print generateUnitList(1/p("1kg*m/s**2"), formatInt)
    ('1', 's2', 'mkg')
    >>> print generateUnitList(p(1, "1/kg/m/s**2"), formatInt)
    ('1', '1', 'kgms2')
    >>> print LaTeXGenerateUnit(1)
    {1.0}
    >>> print LaTeXGenerateUnit(1, formatInt)
    {1}
    >>> print LaTeXGenerateUnit(1, formatFloat)
    {1.000}
    >>> print LaTeXGenerateUnit(p("1m"))
    \unit[{1.0}]{{m}}
    >>> print LaTeXGenerateUnit(p("1m/s"))
    \unitfrac[{1.0}]{{m}}{{s}}
    >>> print LaTeXGenerateUnit(p("1kg*m/s**2"))
    \unitfrac[{1.0}]{{kg}\,{m}}{{s}^{2}}
    >>> print LaTeXGenerateUnit(1/p("1kg*m/s**2"))
    \unitfrac[{1.0}]{{s}^{2}}{{m}\,{kg}}
    >>> print LaTeXGenerateUnit(p(1, "1/kg/m/s**2"))
    \unitfrac[{1.0}]{{1}}{{kg}\,{m}\,{s}^{2}}
    >>> print LaTeXGenerateUnit(p("1m"), formatInt)
    \unit[{1}]{{m}}
    >>> print LaTeXGenerateUnit(p("1m"), formatFloat, 2)
    \unit[{1.00}]{{m}}
    >>> print LaTeXGenerateUnit(p("1m"), formatFloat, 3)
    \unit[{1.000}]{{m}}
    >>> print LaTeXGenerateUnit(p("12345.6789012m"), formatFloat, 5)
    \unit[{12\,345.678\,90}]{{m}}
    >>> print LaTeXGenerateUnit(p("1m/s"), formatInt)
    \unitfrac[{1}]{{m}}{{s}}
    >>> print LaTeXGenerateUnit(p("1kg*m/s**2"), formatInt)
    \unitfrac[{1}]{{kg}\,{m}}{{s}^{2}}
    >>> print LaTeXGenerateUnit(1/p("1kg*m/s**2"), formatInt)
    \unitfrac[{1}]{{s}^{2}}{{m}\,{kg}}
    >>> print LaTeXGenerateUnit(p(1, "1/kg/m/s**2"), formatInt)
    \unitfrac[{1}]{{1}}{{kg}\,{m}\,{s}^{2}}
    >>> 

(this is the test session from UnitPrint).

Cheers

Berthold
-- 
bhoel at starship.python.net / http://starship.python.net/crew/bhoel/
        It is unlawful to use this email address for unsolicited ads
        (USC Title 47 Sec.227). I will assess a US$500 charge for
        reviewing and deleting each unsolicited ad.




More information about the Python-list mailing list