Return string representation of the number in scientific notation. Captures all of the information in the underlying representation.
(self, eng=False, context=None)
| 983 | return "Decimal('%s')" % str(self) |
| 984 | |
| 985 | def __str__(self, eng=False, context=None): |
| 986 | """Return string representation of the number in scientific notation. |
| 987 | |
| 988 | Captures all of the information in the underlying representation. |
| 989 | """ |
| 990 | |
| 991 | sign = ['', '-'][self._sign] |
| 992 | if self._is_special: |
| 993 | if self._exp == 'F': |
| 994 | return sign + 'Infinity' |
| 995 | elif self._exp == 'n': |
| 996 | return sign + 'NaN' + self._int |
| 997 | else: # self._exp == 'N' |
| 998 | return sign + 'sNaN' + self._int |
| 999 | |
| 1000 | # number of digits of self._int to left of decimal point |
| 1001 | leftdigits = self._exp + len(self._int) |
| 1002 | |
| 1003 | # dotplace is number of digits of self._int to the left of the |
| 1004 | # decimal point in the mantissa of the output string (that is, |
| 1005 | # after adjusting the exponent) |
| 1006 | if self._exp <= 0 and leftdigits > -6: |
| 1007 | # no exponent required |
| 1008 | dotplace = leftdigits |
| 1009 | elif not eng: |
| 1010 | # usual scientific notation: 1 digit on left of the point |
| 1011 | dotplace = 1 |
| 1012 | elif self._int == '0': |
| 1013 | # engineering notation, zero |
| 1014 | dotplace = (leftdigits + 1) % 3 - 1 |
| 1015 | else: |
| 1016 | # engineering notation, nonzero |
| 1017 | dotplace = (leftdigits - 1) % 3 + 1 |
| 1018 | |
| 1019 | if dotplace <= 0: |
| 1020 | intpart = '0' |
| 1021 | fracpart = '.' + '0'*(-dotplace) + self._int |
| 1022 | elif dotplace >= len(self._int): |
| 1023 | intpart = self._int+'0'*(dotplace-len(self._int)) |
| 1024 | fracpart = '' |
| 1025 | else: |
| 1026 | intpart = self._int[:dotplace] |
| 1027 | fracpart = '.' + self._int[dotplace:] |
| 1028 | if leftdigits == dotplace: |
| 1029 | exp = '' |
| 1030 | else: |
| 1031 | if context is None: |
| 1032 | context = getcontext() |
| 1033 | exp = ['e', 'E'][context.capitals] + "%+d" % (leftdigits-dotplace) |
| 1034 | |
| 1035 | return sign + intpart + fracpart + exp |
| 1036 | |
| 1037 | def to_eng_string(self, context=None): |
| 1038 | """Convert to a string, using engineering notation if an exponent is needed. |
no test coverage detected