web.commify doesn't work with decimals

Bug #317204 reported by Aaron Swartz
2
Affects Status Importance Assigned to Milestone
web.py
Fix Released
High
Anand Chitipothu

Bug Description

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

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

Revision history for this message
Anand Chitipothu (anandology) wrote : Re: [Bug 317204] Re: web.commify doesn't work with decimals

pushed.

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  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.