diff options
author | Raymond Hettinger <python@rcn.com> | 2016-08-13 18:15:59 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2016-08-13 18:15:59 (GMT) |
commit | 5dd2b8621d8e2b5ef8cecb9a91184bd992c2702d (patch) | |
tree | 9c17e04752e01f938c0a89bb35954f268bffc304 /Lib/_pydecimal.py | |
parent | 0b9e64122b044c71dd1006ea1c257191ead17d4d (diff) | |
parent | f6ffa9826e0dbb319a1627aad004ccf3d8a30c0c (diff) | |
download | cpython-5dd2b8621d8e2b5ef8cecb9a91184bd992c2702d.zip cpython-5dd2b8621d8e2b5ef8cecb9a91184bd992c2702d.tar.gz cpython-5dd2b8621d8e2b5ef8cecb9a91184bd992c2702d.tar.bz2 |
merge
Diffstat (limited to 'Lib/_pydecimal.py')
-rw-r--r-- | Lib/_pydecimal.py | 31 |
1 files changed, 25 insertions, 6 deletions
diff --git a/Lib/_pydecimal.py b/Lib/_pydecimal.py index 7bcf75f..21e875c 100644 --- a/Lib/_pydecimal.py +++ b/Lib/_pydecimal.py @@ -1118,12 +1118,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) @@ -5552,9 +5551,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) |