[Zope3-checkins] CVS: Zope3/src/zope/i18n - format.py:1.14
Garrett Smith
garrett at mojave-corp.com
Fri Mar 26 21:32:19 EST 2004
Update of /cvs-repository/Zope3/src/zope/i18n
In directory cvs.zope.org:/tmp/cvs-serv12605/src/zope/i18n
Modified Files:
format.py
Log Message:
Added support for rounding formatted numbers. E.g. 1.49 formatted with
the pattern "0.0" should be "1.5" -- not "1.4".
=== Zope3/src/zope/i18n/format.py 1.13 => 1.14 ===
--- Zope3/src/zope/i18n/format.py:1.13 Thu Feb 5 17:52:21 2004
+++ Zope3/src/zope/i18n/format.py Fri Mar 26 21:31:48 2004
@@ -250,14 +250,30 @@
max_precision = len(pattern)
min_precision = pattern.count('0')
precision = len(fraction)
+ roundInt = False
if precision > max_precision:
+ round = int(fraction[max_precision]) >= 5
fraction = fraction[:max_precision]
+ if round:
+ if fraction != '':
+ # add 1 to the fraction, maintaining the decimal
+ # precision; if the result >= 1, need to roundInt
+ fractionLen = len(fraction)
+ rounded = int(fraction) + 1
+ fraction = ('%0' + str(fractionLen) + 'i') % rounded
+ if len(fraction) > fractionLen: # rounded fraction >= 1
+ roundInt = True
+ fraction = fraction[1:]
+ else:
+ # fraction missing, e.g. 1.5 -> 1. -- need to roundInt
+ roundInt = True
+
if precision < min_precision:
fraction += self.symbols['nativeZeroDigit']*(min_precision -
precision)
if fraction != '':
fraction = self.symbols['decimal'] + fraction
- return fraction
+ return fraction, roundInt
def format(self, obj, pattern=None):
"See zope.i18n.interfaces.IFormat"
@@ -302,8 +318,12 @@
exp_bin_pattern)
number = ''.join(obj_int_frac)
- number = number[0] + self._format_fraction(number[1:],
+ fraction, roundInt = self._format_fraction(number[1:],
bin_pattern[FRACTION])
+ if roundInt:
+ number = str(int(number[0]) + 1) + fraction
+ else:
+ number = number[0] + fraction
# We might have a plus sign in front of the exponential integer
if not exponent.startswith('-'):
@@ -316,10 +336,13 @@
else:
obj_int_frac = str(obj).split('.')
if len(obj_int_frac) > 1:
- fraction = self._format_fraction(obj_int_frac[1],
+ fraction, roundInt = self._format_fraction(obj_int_frac[1],
bin_pattern[FRACTION])
else:
fraction = ''
+ roundInt = False
+ if roundInt:
+ obj = round(obj)
integer = self._format_integer(str(int(math.fabs(obj))),
bin_pattern[INTEGER])
# Adding grouping
More information about the Zope3-Checkins
mailing list