diff options
author | Raymond Hettinger <python@rcn.com> | 2016-08-13 18:10:23 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2016-08-13 18:10:23 (GMT) |
commit | af0b38f8145636d9caf8da847f8c635c7ccddc30 (patch) | |
tree | 781e72aa777a947b1e9bc86db904df8a1d01ab16 /Lib/decimal.py | |
parent | 0f0ead5c9a031a4676269b465debbc723d757b27 (diff) | |
download | cpython-af0b38f8145636d9caf8da847f8c635c7ccddc30.zip cpython-af0b38f8145636d9caf8da847f8c635c7ccddc30.tar.gz cpython-af0b38f8145636d9caf8da847f8c635c7ccddc30.tar.bz2 |
Issue #27720: Fix error in eng_to_decimal docs and add examples from the specification.
Diffstat (limited to 'Lib/decimal.py')
-rw-r--r-- | Lib/decimal.py | 31 |
1 files changed, 25 insertions, 6 deletions
diff --git a/Lib/decimal.py b/Lib/decimal.py index 4797a7d..e5329dd 100644 --- a/Lib/decimal.py +++ b/Lib/decimal.py @@ -1048,12 +1048,11 @@ class Decimal(object): return sign + intpart + fracpart + exp def to_eng_string(self, context=None): - """Convert to engineering-type string. + """Convert to a string, using engineering notation if an exponent is needed. - Engineering notation has an exponent which is a multiple of 3, so there - are up to 3 digits left of the decimal place. - - Same rules for when in exponential and when as a value as in __str__. + Engineering notation has an exponent which is a multiple of 3. This + can leave up to 3 digits to the left of the decimal place and may + require the addition of either one or two trailing zeros. """ return self.__str__(eng=True, context=context) @@ -5339,9 +5338,29 @@ class Context(object): return r def to_eng_string(self, a): - """Converts a number to a string, using scientific notation. + """Convert to a string, using engineering notation if an exponent is needed. + + Engineering notation has an exponent which is a multiple of 3. This + can leave up to 3 digits to the left of the decimal place and may + require the addition of either one or two trailing zeros. The operation is not affected by the context. + + >>> ExtendedContext.to_eng_string(Decimal('123E+1')) + '1.23E+3' + >>> ExtendedContext.to_eng_string(Decimal('123E+3')) + '123E+3' + >>> ExtendedContext.to_eng_string(Decimal('123E-10')) + '12.3E-9' + >>> ExtendedContext.to_eng_string(Decimal('-123E-12')) + '-123E-12' + >>> ExtendedContext.to_eng_string(Decimal('7E-7')) + '700E-9' + >>> ExtendedContext.to_eng_string(Decimal('7E+1')) + '70' + >>> ExtendedContext.to_eng_string(Decimal('0E+1')) + '0.00E+3' + """ a = _convert_other(a, raiseit=True) return a.to_eng_string(context=self) |