ThousandsCommas.py :  » Web-Frameworks » Aquarium » aquarium-2.3 » aquarium » widget » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Frameworks » Aquarium 
Aquarium » aquarium 2.3 » aquarium » widget » ThousandsCommas.py
"""Add commas to a number."""

## Created: Thu Jan 20 02:37:09 GMT 2005
## Author: Michael Olivier, Shannon -jj Behrens
## Email: jjinux@users.sourceforge.net
##
## Copyright (c) Michael Olivier, Shannon -jj Behrens.  All rights reserved.

from aquarium.util.AquariumClass import AquariumClass


class ThousandsCommas(AquariumClass):

    def __call__(self, n):
        """Return n after putting commas at the thousands marks.

        n
          This is the number to output.

        It's safe to accidentally call this method twice on the same number.

        Note that this widget is only appropriate for English.  For instance,
        in Germany, they reverse the meaning of commas and periods when writing
        a decimal number.

        """
        n = str(n)
        if n.find(",") != -1:
            return n
        if n.find(".") != -1:
            (whole, fractional) = n.split(".")
            whole = self._addCommas(whole, False)
            fractional = self._addCommas(fractional, True)
            return "%s.%s" % (whole, fractional)
        return self._addCommas(n, False)

    def _addCommas(self, s, forward):
        """Add commas to s and return it.

        forward:
          Do we start at the left and work to the right?

        """
        if forward:
            i = 3
            while i < len(s):
                s = "%s,%s" % (s[:i], s[i:])
                i += 4
        else:
            for i in range(len(s) - 3, 0, -3):
                s = "%s,%s" % (s[:i], s[i:])
        return s


# Do some testing.
if __name__ == "__main__":
    from cStringIO import StringIO
    buf = StringIO()
    commas = ThousandsCommas(None)
    for exp in range(10):
        i = 10 ** exp
        print >> buf, commas(i)
    for exp in range(20):
        i = 10 ** exp
        print >> buf, commas("%s.%s" % (i, i))
    print >> buf, commas(commas(5000.0001))
    assert buf.getvalue() == """\
1
10
100
1,000
10,000
100,000
1,000,000
10,000,000
100,000,000
1,000,000,000
1.1
10.10
100.100
1,000.100,0
10,000.100,00
100,000.100,000
1,000,000.100,000,0
10,000,000.100,000,00
100,000,000.100,000,000
1,000,000,000.100,000,000,0
10,000,000,000.100,000,000,00
100,000,000,000.100,000,000,000
1,000,000,000,000.100,000,000,000,0
10,000,000,000,000.100,000,000,000,00
100,000,000,000,000.100,000,000,000,000
1,000,000,000,000,000.100,000,000,000,000,0
10,000,000,000,000,000.100,000,000,000,000,00
100,000,000,000,000,000.100,000,000,000,000,000
1,000,000,000,000,000,000.100,000,000,000,000,000,0
10,000,000,000,000,000,000.100,000,000,000,000,000,00
5,000.000,1
"""
    buf.close()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.