Return string representation of the number in scientific notation. Captures all of the information in the underlying representation.
(self, eng=False, context=None)
| 1039 | return "Decimal('%s')" % str(self) |
| 1040 | |
| 1041 | def __str__(self, eng=False, context=None): |
| 1042 | """Return string representation of the number in scientific notation. |
| 1043 | |
| 1044 | Captures all of the information in the underlying representation. |
| 1045 | """ |
| 1046 | |
| 1047 | sign = ['', '-'][self._sign] |
| 1048 | if self._is_special: |
| 1049 | if self._exp == 'F': |
| 1050 | return sign + 'Infinity' |
| 1051 | elif self._exp == 'n': |
| 1052 | return sign + 'NaN' + self._int |
| 1053 | else: # self._exp == 'N' |
| 1054 | return sign + 'sNaN' + self._int |
| 1055 | |
| 1056 | # number of digits of self._int to left of decimal point |
| 1057 | leftdigits = self._exp + len(self._int) |
| 1058 | |
| 1059 | # dotplace is number of digits of self._int to the left of the |
| 1060 | # decimal point in the mantissa of the output string (that is, |
| 1061 | # after adjusting the exponent) |
| 1062 | if self._exp <= 0 and leftdigits > -6: |
| 1063 | # no exponent required |
| 1064 | dotplace = leftdigits |
| 1065 | elif not eng: |
| 1066 | # usual scientific notation: 1 digit on left of the point |
| 1067 | dotplace = 1 |
| 1068 | elif self._int == '0': |
| 1069 | # engineering notation, zero |
| 1070 | dotplace = (leftdigits + 1) % 3 - 1 |
| 1071 | else: |
| 1072 | # engineering notation, nonzero |
| 1073 | dotplace = (leftdigits - 1) % 3 + 1 |
| 1074 | |
| 1075 | if dotplace <= 0: |
| 1076 | intpart = '0' |
| 1077 | fracpart = '.' + '0'*(-dotplace) + self._int |
| 1078 | elif dotplace >= len(self._int): |
| 1079 | intpart = self._int+'0'*(dotplace-len(self._int)) |
| 1080 | fracpart = '' |
| 1081 | else: |
| 1082 | intpart = self._int[:dotplace] |
| 1083 | fracpart = '.' + self._int[dotplace:] |
| 1084 | if leftdigits == dotplace: |
| 1085 | exp = '' |
| 1086 | else: |
| 1087 | if context is None: |
| 1088 | context = getcontext() |
| 1089 | exp = ['e', 'E'][context.capitals] + "%+d" % (leftdigits-dotplace) |
| 1090 | |
| 1091 | return sign + intpart + fracpart + exp |
| 1092 | |
| 1093 | def to_eng_string(self, context=None): |
| 1094 | """Convert to a string, using engineering notation if an exponent is needed. |
no test coverage detected