web.commify doesn't work with decimals

Bug #317204 reported by Aaron Swartz on 2009-01-14
2
Affects Status Importance Assigned to Milestone
web.py
High
Anand Chitipothu

Bug Description

web.commify('24949.0') should return '24,949.0'

Aaron Swartz (aaronsw) on 2009-01-14
Changed in webpy:
assignee: nobody → anandology
importance: Undecided → High
milestone: none → 0.32
status: New → Confirmed
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):
     """

Changed in webpy:
status: Confirmed → Fix Committed
Changed in webpy:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Other bug subscribers