I am writing Python3 code which requires me to simpy remove the decimal. I tried the most simple thing I could think of
num = int(str(float(x)).replace('.', ''))
where x
is generated using x = a*x*(1-x)
where both a
and x
are float
but when the decimal number is representated in scientific notation it throws the following error:
num = int(str(float(x)).replace('.', ''))
ValueError: invalid literal for int() with base 10: '4234481147613274e-05'
Though it works when it is not representated in scientific notation. What intrigued me was that when I used the same number in command line, it gave me the correct result
>>> x = 4234481147613274e-05
>>> x
42344811476.13274
>>> num = int(str(float(x)).replace('.', ''))
>>> num
4234481147613274
I check the following thread but it didn't help me (Convert scientific notation to decimals). Why is this happening?
EDIT: To be honest, I can't exactly write what I'm coding, as I'm not allowed to disclose it. But I'll still try to make it as clear as possible:
x = 0.55
and a
is a number between 3.85681 and 4, and then I'm looping the value of x
and finally the conversion:
a = 3.85681 + ((big_integer)%14319)*1e-5
for i in range(500): x = a*x*(1-x)
num = int(str(float(x)).replace('.', ''))
What I want is if x = 0.470912345
, I want the num = 470912345
. So I think the problem could be understandable why I didn't use x*10000
or something similar, because I don't want 0.452 to become 4520, but only 452.