[Tutor] Count for loops
eryk sun
eryksun at gmail.com
Wed Apr 12 01:41:01 EDT 2017
On Wed, Apr 12, 2017 at 4:03 AM, boB Stepp <robertvstepp at gmail.com> wrote:
>
> I have not used the decimal module (until tonight). I just now played
> around with it some, but cannot get it to do an exact conversion of
> the number under discussion to a string using str().
Pass a string to the constructor:
>>> d = decimal.Decimal('3.14159265358979323846264338327950288419716939')
>>> str(d)
'3.14159265358979323846264338327950288419716939'
When formatting for printing, note that classic string interpolation
has to first convert the Decimal to a float, which only has 15 digits
of precision (15.95 rounded down).
>>> '%.44f' % d
'3.14159265358979311599796346854418516159057617'
>>> '%.44f' % float(d)
'3.14159265358979311599796346854418516159057617'
The result is more accurate using Python's newer string formatting
system, which allows types to define a custom __format__ method.
>>> '{:.44f}'.format(d)
'3.14159265358979323846264338327950288419716939'
>>> format(d, '.44f')
'3.14159265358979323846264338327950288419716939'
More information about the Tutor
mailing list