str more precise in 3 as 2.7
Mark Lawrence
breamoreboy at yahoo.co.uk
Wed Apr 29 07:50:59 EDT 2015
On 29/04/2015 12:24, Cecil Westerhof wrote:
> I am trying to make my modules also work under Python 3. I found that
> str is more precise in Python 3. The expression:
> str(134 / 6.0)
> gives in 2.7.8:
> '22.3333333333'
> and in 3.4.1:
> '22.333333333333332'
>
> Was not very hard to solve:
> if python_version == 3:
> output06[5] = '22.333333333333332'
> output10[5] = '23.333333333333332'
> output10[8] = '24.77777777777778'
>
> Earlier I did:
> import sys
>
> # For when the difference between 2 and 3 is important
> python_version = sys.version_info[0]
>
> At the moment I use it only once, but it is never bad to be prepared.
>
They simply output different numbers of digits by default. Just control
the number of digits that you want to see and in this case you needn't
worry again about Python 2 vs 3 differences. See either
https://docs.python.org/3/library/string.html#formatspec
or
https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.
Mark Lawrence
More information about the Python-list
mailing list