13 |
# |
# |
14 |
# You should have received a copy of the GNU General Public |
# You should have received a copy of the GNU General Public |
15 |
# License along with program; see the file COPYING. If not, |
# License along with program; see the file COPYING. If not, |
16 |
# write to the Free Software Foundation, Inc., 59 Temple Place |
# write to the Free Software Foundation, Inc., 59 Temple Place |
17 |
# - Suite 330, Boston, MA 02111-1307, USA. |
# - Suite 330, Boston, MA 02111-1307, USA. |
18 |
# |
# |
19 |
# Copyright 2000, 2001 Free Software Foundation |
# Copyright 2000-2003 Free Software Foundation |
20 |
# |
# |
21 |
# FILE: |
# FILE: |
22 |
# GRLayout.py |
# GRLayout.py |
23 |
# |
# |
24 |
# DESCRIPTION: |
# DESCRIPTION: |
25 |
# Class |
# Class |
26 |
# |
# |
27 |
# NOTES: |
# NOTES: |
28 |
# |
# |
29 |
# HISTORY: |
# HISTORY: |
30 |
# |
# |
31 |
|
|
32 |
|
from types import * |
33 |
|
|
34 |
|
def applyFormatting (value, mask): |
|
def applyFormatting (value, mask): |
|
35 |
# This obviously doesn't do anything with the mask yet |
# This obviously doesn't do anything with the mask yet |
36 |
# Just returns a string |
# Just returns a string |
37 |
if value == None: |
if value == None: |
38 |
return "" |
return "" |
39 |
else: |
elif mask: |
40 |
return "%s" % value |
|
41 |
|
# TODO: This whole section should be using FormatMasks |
42 |
|
# TODO: This is all a gross hack!!!! |
43 |
|
try: |
44 |
|
return value.strftime(mask) |
45 |
|
except AttributeError: |
46 |
|
pass |
47 |
|
|
48 |
|
# TODO: As said above, this is a bad hack w/a lot of assumptions |
49 |
|
if type(value) == FloatType: |
50 |
|
v = mask.split('.',1) |
51 |
|
try: |
52 |
|
dec = len(v[1]) |
53 |
|
except: |
54 |
|
dec = 0 |
55 |
|
comma = mask.find(',') + 1 |
56 |
|
|
57 |
|
if v[0][:1] == '$': |
58 |
|
rv = "$" |
59 |
|
else: |
60 |
|
rv = "" |
61 |
|
|
62 |
|
push = 10 ** dec |
63 |
|
|
64 |
|
fract = int(int(value * push) - int(value) * push + 0.5) |
65 |
|
whole = int(value) |
66 |
|
|
67 |
|
wstr = str(whole) |
68 |
|
|
69 |
|
if comma: |
70 |
|
commas, leading = divmod(len(wstr),3) |
71 |
|
if not leading and commas: |
72 |
|
commas -= 1 |
73 |
|
leading += 3 |
74 |
|
if leading: |
75 |
|
rv += wstr[:leading] |
76 |
|
|
77 |
|
wstr = wstr[leading:] |
78 |
|
for i in range(commas): |
79 |
|
rv += ',' + wstr[:3] |
80 |
|
wstr = wstr[3:] |
81 |
|
|
82 |
|
if dec: |
83 |
|
rv += '.' + ("%%0%sd" % dec) % fract |
84 |
|
return rv |
85 |
|
|
86 |
|
return "%s" % value |
87 |
|
|
88 |
|
|
89 |
|
if __name__ == '__main__': |
90 |
|
print applyFormatting(1.444,'#,##0.00') |
91 |
|
print applyFormatting(12.444,'#,##0.00') |
92 |
|
print applyFormatting(123.444,'#,##0.00') |
93 |
|
print applyFormatting(1234.444,'#,##0.00') |
94 |
|
print applyFormatting(12345.444,'#,##0.00') |
95 |
|
print applyFormatting(123456.444,'#,##0.00') |
96 |
|
# print applyFormatting(1234567.444,'#,##0.00') |
97 |
|
# print applyFormatting(1234567.444,'#,##0.0000') |
98 |
|
# print applyFormatting(1234567.444,'#,##0') |