web.commify doesn't work with decimals
Bug #317204 reported by
Aaron Swartz
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
web.py |
Fix Released
|
High
|
Anand Chitipothu |
Bug Description
web.commify(
Changed in webpy: | |
assignee: | nobody → anandology |
importance: | Undecided → High |
milestone: | none → 0.32 |
status: | New → Confirmed |
Changed in webpy: | |
status: | Confirmed → Fix Committed |
Changed in webpy: | |
status: | Fix Committed → Fix Released |
To post a comment you must log in.
Here's a patch:
diff --git a/web/utils.py b/web/utils.py
'1,234, 567,890'
index 73a2ba0..2bb5da9 100755
--- a/web/utils.py
+++ b/web/utils.py
@@ -636,17 +636,35 @@ def commify(n):
'1,234'
>>> commify(1234567890)
+ >>> 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)
>>>
""" reversed( str(n)) ): reversed( dollars) ):
r. insert( 0, ',')
r.insert( 0, c)
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(
+ for i, c in enumerate(
if i and (not (i % 3)):
- return ''.join(r)
+ out = ''.join(r)
+ if cents:
+ out += '.' + cents
+ return out
def dateify( datestring) :
"""