Comment 1 for bug 317204

Revision history for this message
Aaron Swartz (aaronsw) wrote :

Here's a patch:

diff --git a/web/utils.py b/web/utils.py
index 73a2ba0..2bb5da9 100755
--- a/web/utils.py
+++ b/web/utils.py
@@ -636,17 +636,35 @@ def commify(n):
         '1,234'
         >>> commify(1234567890)
         '1,234,567,890'
+ >>> commify(123.0)
+ '123.0'
+ >>> commify(1234.5)
+ '1,234.5'
+ >>> commify(1234.56789)
+ '1,234.56789'
+ >>> commify('%.2f' % 1234.5)
+ '1,234.50'
         >>> commify(None)
         >>>

     """
     if n is None: return None
+
+ n = str(n)
+ if '.' in n:
+ dollars, cents = n.split('.')
+ else:
+ dollars, cents = n, None
+
     r = []
- for i, c in enumerate(reversed(str(n))):
+ for i, c in enumerate(reversed(dollars)):
         if i and (not (i % 3)):
             r.insert(0, ',')
         r.insert(0, c)
- return ''.join(r)
+ out = ''.join(r)
+ if cents:
+ out += '.' + cents
+ return out

 def dateify(datestring):
     """