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

Skip to content

Commit 5dd2b86

Browse files
committed
merge
2 parents 0b9e641 + f6ffa98 commit 5dd2b86

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
@@ -849,11 +849,13 @@ Decimal objects
849849

850850
.. method:: to_eng_string(context=None)
851851

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

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

858860
.. method:: to_integral(rounding=None, context=None)
859861

@@ -1423,7 +1425,11 @@ In addition to the three supplied contexts, new contexts can be created with the
14231425

14241426
.. method:: to_eng_string(x)
14251427

1426-
Converts a number to a string, using scientific notation.
1428+
Convert to a string, using engineering notation if an exponent is needed.
1429+
1430+
Engineering notation has an exponent which is a multiple of 3. This
1431+
can leave up to 3 digits to the left of the decimal place and may
1432+
require the addition of either one or two trailing zeros.
14271433

14281434

14291435
.. method:: to_integral_exact(x)

Lib/_pydecimal.py

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

11201120
def to_eng_string(self, context=None):
1121-
"""Convert to engineering-type string.
1121+
"""Convert to a string, using engineering notation if an exponent is needed.
11221122
1123-
Engineering notation has an exponent which is a multiple of 3, so there
1124-
are up to 3 digits left of the decimal place.
1125-
1126-
Same rules for when in exponential and when as a value as in __str__.
1123+
Engineering notation has an exponent which is a multiple of 3. This
1124+
can leave up to 3 digits to the left of the decimal place and may
1125+
require the addition of either one or two trailing zeros.
11271126
"""
11281127
return self.__str__(eng=True, context=context)
11291128

@@ -5552,9 +5551,29 @@ def subtract(self, a, b):
55525551
return r
55535552

55545553
def to_eng_string(self, a):
5555-
"""Converts a number to a string, using scientific notation.
5554+
"""Convert to a string, using engineering notation if an exponent is needed.
5555+
5556+
Engineering notation has an exponent which is a multiple of 3. This
5557+
can leave up to 3 digits to the left of the decimal place and may
5558+
require the addition of either one or two trailing zeros.
55565559
55575560
The operation is not affected by the context.
5561+
5562+
>>> ExtendedContext.to_eng_string(Decimal('123E+1'))
5563+
'1.23E+3'
5564+
>>> ExtendedContext.to_eng_string(Decimal('123E+3'))
5565+
'123E+3'
5566+
>>> ExtendedContext.to_eng_string(Decimal('123E-10'))
5567+
'12.3E-9'
5568+
>>> ExtendedContext.to_eng_string(Decimal('-123E-12'))
5569+
'-123E-12'
5570+
>>> ExtendedContext.to_eng_string(Decimal('7E-7'))
5571+
'700E-9'
5572+
>>> ExtendedContext.to_eng_string(Decimal('7E+1'))
5573+
'70'
5574+
>>> ExtendedContext.to_eng_string(Decimal('0E+1'))
5575+
'0.00E+3'
5576+
55585577
"""
55595578
a = _convert_other(a, raiseit=True)
55605579
return a.to_eng_string(context=self)

0 commit comments

Comments
 (0)