Vincent_Cos Vincent_Cos -4 years ago 162
Python Question

Print a large integer in Python

I'd like to print an large integer without the "e+XX" at the end in Python.

For example, when I write:

result = 1;
i = 0;

while i < k:
result = result * (n - i) / (i + 1)
i += 1

The result is 3.066451080298821e+19, and I would like to have 30664510802988208300.

Answer Source

I think the question you really want to ask is 'how can I print a number in Python without scientific notation?'

The answer is, your number right now is a float. Try print(type(result)) and you will see it says float. You could type cast it to an integer by doing int(result), and it will show close to the full number, 30664510802988208128. It will be a bit off because of the memory size storage limitations of int vs float.

The better way to do this would be like:

result = 1
i = 0

while i < 18:
    result = result * (100 - i) // (i + 1)
    i += 1


which will keep result as an int type. It now should print 30664510802988208300

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download