1 Jul 2010 15:11
Re: puzzled by Python 3's print()
Richard D. Moores <rdmoores <at> gmail.com>
2010-07-01 13:11:21 GMT
2010-07-01 13:11:21 GMT
On Thu, Jul 1, 2010 at 04:57, Steven D'Aprano <steve <at> pearwood.info> wrote: > On Thu, 1 Jul 2010 06:26:21 pm Richard D. Moores wrote: >> >>> x = 2000000000000034 >> >>> x/2 >> 1000000000000017.0 >> >> >>> print(x/2) >> 1e+15 >> >> I was expecting, in fact needing, 1000000000000000017 or >> 1000000000000000017.0 >> >> 1e+15 is unsatisfactory. Am I forced to use the decimal module? > > This is not an issue with print, this is an issue with floats -- they > produced a rounded, approximate value when converted to a string. print > merely prints that string: > >>>> x = 1e15 +17 >>>> x > 1000000000000017.0 >>>> print(x) > 1e+15 >>>> str(x) > '1e+15' > > > If you want more control over the string conversion, you can do > something like this: >(Continue reading)
RSS Feed