A lot of confusion is caused by the print function converting an integer or
float
to a string before printing to console. thus both '1234 and '1234' are
shown as
1234 on the console. Similarly '15.4' and 15.4 are displayed as 15.4. There
is no way
to tell which is a string, which is an int and which is a float by looking
at the display on
console. In order to find which is which one has to type the variables.


>>> s = '1234'

>>> print(s)
1234

>>> s = 1234

>>> print(s)
1234

>>> s = '15.4'

>>> print(s)
15.4

>>> s = 15.4

>>> print(s)
15.4

regards,
Sarma.

On Wed, Apr 12, 2017 at 11:11 AM, eryk sun <eryk...@gmail.com> wrote:

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

Reply via email to