How do I force Python to keep an integer out of scientific notation
-
04-07-2021 - |
Question
I am trying to write a method in Python 3.2 that encrypts a phrase and then decrypts it. The problem is that the numbers are so big that when Python does math with them it immediately converts it into scientific notation. Since my code requires all the numbers to function scientific notation, this is not useful.
What I have is:
coded = ((eval(input(':'))+1213633288469888484)/2)+1042
Basically, I just get a number from the user and do some math to it.
I have tried format()
and a couple other things but I can't get them to work.
EDIT: I use only even integers.
Solution
In python3, '/'
does real division (e.g. floating point). To get integer division, you need to use //
. In other words 100/2
yields 50.0
(float) whereas 100//2
yields 50
(integer)
Your code probably needs to be changed as:
coded = ((eval(input(':'))+1213633288469888484)//2)+1042
As a cautionary tale however, you may want to consider using int
instead of eval
:
coded = ((int(input(':'))+1213633288469888484)//2)+1042
OTHER TIPS
If you know that the floating point value is really an integer, or you don't care about dropping the fractional part, you can just convert it to an int
before you print it.
>>> print 1.2e16
1.2e+16
>>> print int(1.2e16)
12000000000000000