diff options
author | Martin v. Löwis <martin@v.loewis.de> | 2001-04-13 08:09:50 (GMT) |
---|---|---|
committer | Martin v. Löwis <martin@v.loewis.de> | 2001-04-13 08:09:50 (GMT) |
commit | 88ad12afacb2b08b5d675be61f47c9244fba6825 (patch) | |
tree | 11c33f6da93ea64129a0e465a95e0326372e3662 | |
parent | 613b2222cf859561adf00ebd5f67d3fc9738f752 (diff) | |
download | cpython-88ad12afacb2b08b5d675be61f47c9244fba6825.zip cpython-88ad12afacb2b08b5d675be61f47c9244fba6825.tar.gz cpython-88ad12afacb2b08b5d675be61f47c9244fba6825.tar.bz2 |
Patch #415777: new grouping strategy.
fixes bug #414940, and redoes the fix for #129417 in a different way.
It also fixes a number of other problems with locale-specific formatting:
If there is leading or trailing spaces, then no grouping should be applied
in the spaces, and the total length of the string should not be changed
due to grouping.
Also added test case which works only if the en_US locale is available.
-rw-r--r-- | Lib/locale.py | 40 | ||||
-rw-r--r-- | Lib/test/output/test_locale | 1 | ||||
-rw-r--r-- | Lib/test/test_locale.py | 37 |
3 files changed, 68 insertions, 10 deletions
diff --git a/Lib/locale.py b/Lib/locale.py index d26560d..f88803c 100644 --- a/Lib/locale.py +++ b/Lib/locale.py @@ -95,6 +95,12 @@ def _group(s): grouping=conv['grouping'] if not grouping:return s result="" + seps = 0 + spaces = "" + if s[-1] == ' ': + sp = s.find(' ') + spaces = s[sp:] + s = s[:sp] while s and grouping: # if grouping is -1, we are done if grouping[0]==CHAR_MAX: @@ -106,34 +112,48 @@ def _group(s): grouping=grouping[1:] if result: result=s[-group:]+conv['thousands_sep']+result + seps += 1 else: result=s[-group:] s=s[:-group] + if s and s[-1] not in "0123456789": + # the leading string is only spaces and signs + return s+result+spaces,seps if not result: - return s + return s+spaces,seps if s: result=s+conv['thousands_sep']+result - return result + seps += 1 + return result+spaces,seps def format(f,val,grouping=0): """Formats a value in the same way that the % formatting would use, but takes the current locale into account. Grouping is applied if the third parameter is true.""" - result = f % abs(val) + result = f % val fields = result.split(".") + seps = 0 if grouping: - fields[0]=_group(fields[0]) + fields[0],seps=_group(fields[0]) if len(fields)==2: - res = fields[0]+localeconv()['decimal_point']+fields[1] + result = fields[0]+localeconv()['decimal_point']+fields[1] elif len(fields)==1: - res = fields[0] + result = fields[0] else: raise Error, "Too many decimal points in result string" - if val < 0: - return '-'+res - else: - return res + while seps: + # If the number was formatted for a specific width, then it + # might have been filled with spaces to the left or right. If + # so, kill as much spaces as there where separators. + # Leading zeroes as fillers are not yet dealt with, as it is + # not clear how they should interact with grouping. + sp = result.find(" ") + if sp==-1:break + result = result[:sp]+result[sp+1:] + seps -= 1 + + return result def str(val): """Convert float to integer, taking the locale into account.""" diff --git a/Lib/test/output/test_locale b/Lib/test/output/test_locale new file mode 100644 index 0000000..602825e --- /dev/null +++ b/Lib/test/output/test_locale @@ -0,0 +1 @@ +test_locale diff --git a/Lib/test/test_locale.py b/Lib/test/test_locale.py new file mode 100644 index 0000000..4fb042a --- /dev/null +++ b/Lib/test/test_locale.py @@ -0,0 +1,37 @@ +from test_support import verbose +import locale + +oldlocale = locale.setlocale(locale.LC_NUMERIC) + +try: + locale.setlocale(locale.LC_NUMERIC, "en_US") +except locale.Error: + raise ImportError, "test locale en_US not supported" + +def testformat(formatstr, value, grouping = 0, output=None): + if verbose: + if output: + print "%s %% %s =? %s ..." %\ + (repr(formatstr), repr(value), repr(output)), + else: + print "%s %% %s works? ..." % (repr(formatstr), repr(value)), + result = locale.format(formatstr, value, grouping = grouping) + if output and result != output: + if verbose: + print 'no' + print "%s %% %s == %s != %s" %\ + (repr(formatstr), repr(value), repr(result), repr(output)) + else: + if verbose: + print "yes" + +try: + testformat("%f", 1024, grouping=1, output='1,024.000000') + testformat("%f", 102, grouping=1, output='102.000000') + testformat("%f", -42, grouping=1, output='-42.000000') + testformat("%+f", -42, grouping=1, output='-42.000000') + testformat("%20.f", -42, grouping=1, output=' -42') + testformat("%+10.f", -4200, grouping=1, output=' -4,200') + testformat("%-10.f", 4200, grouping=1, output='4,200 ') +finally: + locale.setlocale(locale.LC_NUMERIC, oldlocale) |