python - Converting number in scientific notation to int -
could explain why can not use int()
convert integer number represented in string-scientific notation python int
?
for example not work:
print int('1e1')
but does:
print int(float('1e1')) print int(1e1) # works
why int
not recognise string integer? surely simple checking sign of exponent?
behind scenes scientific number notation represented float internally. reason varying number range integer maps fixed value range, let's 2^32
values. scientific representation similar floating representation significant , exponent. further details can lookup in https://en.wikipedia.org/wiki/floating_point.
you cannot cast scientific number representation string integer directly.
print int(1e1) # works
works because 1e1
number float.
>>> type(1e1) <type 'float'>
back question: want integer float or scientific string. details: https://docs.python.org/2/reference/lexical_analysis.html#integers
>>> int("13.37") traceback (most recent call last): file "<stdin>", line 1, in <module> valueerror: invalid literal int() base 10: '13.37'
for float or scientific representations have use intermediate step on float
.
Comments
Post a Comment