Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f6ffa98

Browse files
committed
Issue #27720: Fix error in eng_to_decimal docs and add examples from the specification.
(Based on a first draft patch from Evelyn Mitchell.)
1 parent bd66435 commit f6ffa98

2 files changed

Lines changed: 36 additions & 11 deletions

File tree

Doc/library/decimal.rst

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -836,11 +836,13 @@ Decimal objects
836836

837837
.. method:: to_eng_string(context=None)
838838

839-
Convert to an engineering-type string.
839+
Convert to a string, using engineering notation if an exponent is needed.
840840

841-
Engineering notation has an exponent which is a multiple of 3, so there
842-
are up to 3 digits left of the decimal place. For example, converts
843-
``Decimal('123E+1')`` to ``Decimal('1.23E+3')``.
841+
Engineering notation has an exponent which is a multiple of 3. This
842+
can leave up to 3 digits to the left of the decimal place and may
843+
require the addition of either one or two trailing zeros.
844+
845+
For example, this converts ``Decimal('123E+1')`` to ``Decimal('1.23E+3')``.
844846

845847
.. method:: to_integral(rounding=None, context=None)
846848

@@ -1410,7 +1412,11 @@ In addition to the three supplied contexts, new contexts can be created with the
14101412

14111413
.. method:: to_eng_string(x)
14121414

1413-
Converts a number to a string, using scientific notation.
1415+
Convert to a string, using engineering notation if an exponent is needed.
1416+
1417+
Engineering notation has an exponent which is a multiple of 3. This
1418+
can leave up to 3 digits to the left of the decimal place and may
1419+
require the addition of either one or two trailing zeros.
14141420

14151421

14161422
.. method:: to_integral_exact(x)

Lib/_pydecimal.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,12 +1068,11 @@ def __str__(self, eng=False, context=None):
10681068
return sign + intpart + fracpart + exp
10691069

10701070
def to_eng_string(self, context=None):
1071-
"""Convert to engineering-type string.
1071+
"""Convert to a string, using engineering notation if an exponent is needed.
10721072
1073-
Engineering notation has an exponent which is a multiple of 3, so there
1074-
are up to 3 digits left of the decimal place.
1075-
1076-
Same rules for when in exponential and when as a value as in __str__.
1073+
Engineering notation has an exponent which is a multiple of 3. This
1074+
can leave up to 3 digits to the left of the decimal place and may
1075+
require the addition of either one or two trailing zeros.
10771076
"""
10781077
return self.__str__(eng=True, context=context)
10791078

@@ -5502,9 +5501,29 @@ def subtract(self, a, b):
55025501
return r
55035502

55045503
def to_eng_string(self, a):
5505-
"""Converts a number to a string, using scientific notation.
5504+
"""Convert to a string, using engineering notation if an exponent is needed.
5505+
5506+
Engineering notation has an exponent which is a multiple of 3. This
5507+
can leave up to 3 digits to the left of the decimal place and may
5508+
require the addition of either one or two trailing zeros.
55065509
55075510
The operation is not affected by the context.
5511+
5512+
>>> ExtendedContext.to_eng_string(Decimal('123E+1'))
5513+
'1.23E+3'
5514+
>>> ExtendedContext.to_eng_string(Decimal('123E+3'))
5515+
'123E+3'
5516+
>>> ExtendedContext.to_eng_string(Decimal('123E-10'))
5517+
'12.3E-9'
5518+
>>> ExtendedContext.to_eng_string(Decimal('-123E-12'))
5519+
'-123E-12'
5520+
>>> ExtendedContext.to_eng_string(Decimal('7E-7'))
5521+
'700E-9'
5522+
>>> ExtendedContext.to_eng_string(Decimal('7E+1'))
5523+
'70'
5524+
>>> ExtendedContext.to_eng_string(Decimal('0E+1'))
5525+
'0.00E+3'
5526+
55085527
"""
55095528
a = _convert_other(a, raiseit=True)
55105529
return a.to_eng_string(context=self)

0 commit comments

Comments
 (0)