MCPcopy Index your code
hub / github.com/RustPython/RustPython / __str__

Method __str__

Lib/_pydecimal.py:982–1032  ·  view source on GitHub ↗

Return string representation of the number in scientific notation. Captures all of the information in the underlying representation.

(self, eng=False, context=None)

Source from the content-addressed store, hash-verified

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

Callers 2

to_eng_stringMethod · 0.95
to_sci_stringMethod · 0.45

Calls 2

lenFunction · 0.85
getcontextFunction · 0.85

Tested by

no test coverage detected