Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #109011 > unrolled thread
| Started by | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| First post | 2016-05-23 11:05 -0600 |
| Last post | 2016-05-23 11:05 -0600 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: str(float) python 3 versus 2.7 Ian Kelly <ian.g.kelly@gmail.com> - 2016-05-23 11:05 -0600
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2016-05-23 11:05 -0600 |
| Subject | Re: str(float) python 3 versus 2.7 |
| Message-ID | <mailman.27.1464023143.20402.python-list@python.org> |
On Mon, May 23, 2016 at 10:39 AM, Robin Becker <robin@reportlab.com> wrote:
> I had always imagined that the str founction did some kind of rounding on
> floats to prevent small numerical errors from showing up. The 2.7
> documentation starts like this
>
>> class str(object='')
>> Return a string containing a nicely printable representation of an object.
>> For s
>
>
>
>
> However, I see a difference in the behaviour of python3.3, 3.4 & 3.5 when
> compared to python 2.7.
>
>> C:\Users\rptlab>\python33\python.exe -c"print(str(3*0.2))"
>> 0.6000000000000001
>>
>> C:\Users\rptlab>\python34\python.exe -c"print(str(3*0.2))"
>> 0.6000000000000001
>>
>> C:\Users\rptlab>\python35\python.exe -c"print(str(3*0.2))"
>> 0.6000000000000001
>>
>> C:\Users\rptlab>\python27\python.exe -c"print(str(3*0.2))"
>> 0.6
>
>
> I suppose I am being naive and should use the round function when computing
> tick labels, but that leads to other issues.
>
> Is there a sensible way to take a set of floats and find a suitable format
> to show significant figures for all, but leave off the noise?
This was changed in 3.2; see https://bugs.python.org/issue9337
If you want to show the float in a less noisy format, you can
explicitly format it using the 'g' or 'n' presentation type, which
essentially round to a given precision and strip off trailing zeros:
>>> '{:g}'.format(3*0.2)
'0.6'
It may also be worthwhile to read the section at
https://docs.python.org/3/library/string.html#format-specification-mini-language
that covers the precise formatting rules used by this.
Back to top | Article view | comp.lang.python
csiph-web