Search code examples
pythonpython-3.xnumbersbiginteger

How to return a big number in a plain format?


I want to return a big number in a plain format but instead I'm getting this:

>>> x=1000000000000000000000000000000
>>> int(1*x/100) # try to get 1% of x
9999999999999999583119736832 # <------------ here

I was expecting 10000000000000000000000000000, how do I achieve this?


Solution

  • You can use decimal to handle the decimal point more precisely.

    It provides fast correctly-rounded decimal floating point arithmetic. In contrast, numbers like 1.1 and 2.2 do not have exact representations in binary floating point. Decimal numbers can be represented exactly and in decimal floating point.

    from decimal import Decimal
    x = Decimal(1000000000000000000000000000000)
    print(int(1*x/100))  # 10000000000000000000000000000