Float to Int type conversion in Python for large integers/numbers

Viewed 24

Need some help on the below piece of code that I am working on. Why original number in "a" is different from "c" when it goes through a type conversion. Any way we can make "a" and "c" same when it goes through float -> int type conversion?

a = '46700000000987654321'
b = float(a)  => 4.670000000098765e+19
c = int(b)    => 46700000000987652096
a == c        => False
1 Answers

Please read this document about Floating Point Arithmetic: Issues and Limitations :

https://docs.python.org/3/tutorial/floatingpoint.html

for your example:

from decimal import Decimal
a='46700000000987654321'

b=Decimal(a)

print(b)    #46700000000987654321

c=int(b)

print(c)    #46700000000987654321
Related