On Wed, Apr 12, 2017 at 4:03 AM, boB Stepp <[email protected]> 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'
_______________________________________________
Tutor maillist - [email protected]
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor