commit-gnue
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

gnue-common/src/utils TextUtils.py


From: Jason Cater
Subject: gnue-common/src/utils TextUtils.py
Date: Sun, 27 Jul 2003 19:55:23 -0400

CVSROOT:        /cvsroot/gnue
Module name:    gnue-common
Branch:         
Changes by:     Jason Cater <address@hidden>    03/07/27 19:55:23

Modified files:
        src/utils      : TextUtils.py 

Log message:
        Added a "comify" function to format numbers with commas

CVSWeb URLs:
http://savannah.gnu.org/cgi-bin/viewcvs/gnue/gnue-common/src/utils/TextUtils.py.diff?tr1=1.7&tr2=1.8&r1=text&r2=text

Patches:
Index: gnue-common/src/utils/TextUtils.py
diff -c gnue-common/src/utils/TextUtils.py:1.7 
gnue-common/src/utils/TextUtils.py:1.8
*** gnue-common/src/utils/TextUtils.py:1.7      Sat Jun  7 03:22:56 2003
--- gnue-common/src/utils/TextUtils.py  Sun Jul 27 19:55:23 2003
***************
*** 126,128 ****
--- 126,156 ----
  _thirdDollarMap = ('', 'Thousand ', 'Million ', 'Billion ', 'Trillion ')
  
  
+ 
+ # Comify a number
+ # (e.g., print -9900 as -9,900.00)
+ def comify(num, decimals=2, parenthesis=0):
+   whole, dec = (string.split(string.strip(("%%12.%sf" % decimals) % 
abs(num)),'.') + [""])[:2]
+   if len(dec):
+     dec = "." + dec
+ 
+   s = ""
+ 
+   for i in range(divmod(len(whole),3)[0]+1):
+     j = len(whole) - i*3
+     s = "," + whole[j > 3 and j-3 or 0:j] + s
+ 
+   s += dec
+ 
+   while s[:1] == ',':
+     s = s[1:]
+ 
+   if num < 0:
+     if parenthesis:
+       s = "(%s)" % s
+     else:
+       s = "-" + s
+   elif parenthesis:
+     s += " "
+ 
+   return s
\ No newline at end of file




reply via email to

[Prev in Thread] Current Thread [Next in Thread]