0

I wrote those simple lines and run them on python 2/3.

i=1.194857193845710948754654
print(len(str(i)))

I got 2 different outputs. What is the reason for that? and how could I achieve the same output in python2 ?

Output python2:

>>> 13

Output python3:

>>> 18
1

1 Answer 1

3

It's because of different implementation of str() for float, as can easily be verified.

The actual float precision is the same.

$ python2
Python 2.7.9
>>> i=1.194857193845710948754654
>>> i
1.1948571938457109
>>> str(i)
'1.19485719385'
>>> 
$ python3
Python 3.4.3
>>> i=1.194857193845710948754654
>>> i
1.1948571938457109
>>> str(i)
'1.1948571938457109'
>>> 
Sign up to request clarification or add additional context in comments.

1 Comment

thanks a lot, this solved my problem. I didnt thought the string conversion could be the limiting factor :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.