commit-gnue
[Top][All Lists]
Advanced

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

gnue/common/src GDataFormatter.py


From: Jason Cater
Subject: gnue/common/src GDataFormatter.py
Date: Wed, 01 Jan 2003 15:47:51 -0500

CVSROOT:        /cvsroot/gnue
Module name:    gnue
Changes by:     Jason Cater <address@hidden>    03/01/01 15:47:51

Modified files:
        common/src     : GDataFormatter.py 

Log message:
        added basic number formatting to reports (HACK ALERT)

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

Patches:
Index: gnue/common/src/GDataFormatter.py
diff -c gnue/common/src/GDataFormatter.py:1.6 
gnue/common/src/GDataFormatter.py:1.7
*** gnue/common/src/GDataFormatter.py:1.6       Sun Dec 22 22:44:17 2002
--- gnue/common/src/GDataFormatter.py   Wed Jan  1 15:47:51 2003
***************
*** 13,43 ****
  #
  # You should have received a copy of the GNU General Public 
  # License along with program; see the file COPYING. If not, 
! # write to the Free Software Foundation, Inc., 59 Temple Place 
  # - Suite 330, Boston, MA 02111-1307, USA.
  #
! # Copyright 2000, 2001 Free Software Foundation
  #
  # FILE:
  # GRLayout.py
  #
  # DESCRIPTION:
! # Class 
  #
  # NOTES:
  #
  # HISTORY:
  #
  
  def applyFormatting (value, mask):
    # This obviously doesn't do anything with the mask yet
    # Just returns a string
    if value == None:
      return ""
    elif mask:
      try:
        return value.strftime(mask)
      except AttributeError:
        pass
  
    return "%s" % value
--- 13,98 ----
  #
  # You should have received a copy of the GNU General Public 
  # License along with program; see the file COPYING. If not, 
! # write to the Free Software Foundation, Inc., 59 Temple Place
  # - Suite 330, Boston, MA 02111-1307, USA.
  #
! # Copyright 2000-2002 Free Software Foundation
  #
  # FILE:
  # GRLayout.py
  #
  # DESCRIPTION:
! # Class
  #
  # NOTES:
  #
  # HISTORY:
  #
  
+ from types import *
+ 
  def applyFormatting (value, mask):
    # This obviously doesn't do anything with the mask yet
    # Just returns a string
    if value == None:
      return ""
    elif mask:
+   
+     # TODO: This whole section should be using FormatMasks
+     # TODO: This is all a gross hack!!!!
      try:
        return value.strftime(mask)
      except AttributeError:
        pass
  
+     # TODO: As said above, this is a bad hack w/a lot of assumptions
+     if type(value) == FloatType:
+       v = mask.split('.',1)
+       try:
+         dec = len(v[1])
+       except:
+         dec = 0
+       comma = mask.find(',') + 1
+ 
+       if v[:1] == '$':
+         rv = "$"
+       else:
+         rv = ""
+ 
+       push = 10 ** dec
+ 
+       fract = int(int(value * push) - int(value) * push + 0.5)
+       whole = int(value)
+ 
+       wstr = str(whole)
+ 
+       if comma:
+         commas, leading = divmod(len(wstr),3)
+         if not leading and commas:
+           commas -= 1
+           leading += 3
+         if leading:
+           rv += wstr[:leading]
+ 
+         wstr = wstr[leading:]
+         for i in range(commas):
+           rv += ',' + wstr[:3]
+           wstr = wstr[3:]
+ 
+       if dec:
+         rv += '.' + ("%%0%sd" % dec) % fract
+       return rv
+ 
    return "%s" % value
+ 
+ 
+ if __name__ == '__main__':
+   print applyFormatting(1.444,'#,##0.00')
+   print applyFormatting(12.444,'#,##0.00')
+   print applyFormatting(123.444,'#,##0.00')
+   print applyFormatting(1234.444,'#,##0.00')
+   print applyFormatting(12345.444,'#,##0.00')
+   print applyFormatting(123456.444,'#,##0.00')
+ #  print applyFormatting(1234567.444,'#,##0.00')
+ #  print applyFormatting(1234567.444,'#,##0.0000')
+ #  print applyFormatting(1234567.444,'#,##0')



reply via email to

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