Home > front end >  How to return a big number in a plain format?
How to return a big number in a plain format?

Time:06-23

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?

CodePudding user response:

Floating point arithmetic has Issues and Limitations.

In your case simply avoid it by using integer division:

>>> x=1000000000000000000000000000000
>>> x=x//100
>>> x
10000000000000000000000000000

CodePudding user response:

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
  • Related